【问题标题】:Middleman: run custom action after build中间人:构建后运行自定义操作
【发布时间】:2014-05-26 23:42:27
【问题描述】:

如何在中间人构建页面后运行自定义操作(例如,将文件复制到构建文件夹)?

我想将Readme.md 文件从源代码放到构建目录。

【问题讨论】:

  • 对middleman不熟悉,但是看起来好像是在终端运行的,所以你可以制作一个bash脚本来调用middleman,然后将文件移动到正确的文件夹中。
  • 我猜你也指外部源代码 - 因为默认情况下源文件夹中的所有文件都将被复制到构建中。

标签: ruby middleman


【解决方案1】:

你可以使用after_build钩子。将以下代码添加到config.rb

你可以使用的钩子写在https://middlemanapp.com/advanced/custom_extensions/中。

虽然没有很好的文档记录,但似乎after_build可以直接在config.rb中使用,无需编写自己的扩展。

after_build do |builder|
  src = File.join(config[:source],"Readme.md")
  dst = File.join(config[:build_dir],"Readme.md")
  builder.thor.source_paths << File.dirname(__FILE__)
  builder.thor.copy_file(src,dst)
end

【讨论】:

    【解决方案2】:

    虽然after_build 挂钩是默认答案,但我建议使用任务运行器来完成这项工作。

    任务运行器非常棒,可以让这些例程变得更容易。例如,大多数 Middleman 项目需要部署到托管服务器。因此,如果您碰巧使用任务运行器进行部署,您也可以使用它来复制文件。

    如果您不使用任务运行器,请考虑使用一个。它会为您省去很多麻烦。

    Rake 是 Middleman 的 Ruby 环境的自然选择,但我更喜欢 Grunt

    这是一个 Grunt 复制任务(使用 grunt-contrib-copy 插件):

    copy: {
      bowercomponents: {
        files: [
          {
            expand: true,
            flatten: true,
            src: [
              'source/Readme.md'
            ],
            dest: 'build/',
            filter: 'isFile'
          }
        ]
      }
    }
    

    这是一个使用 grunt-shell 插件的部署任务:

    shell: {
    
        buildAndPublish: {
            command: [
              'echo "### Building ###"',
              'bundle exec middleman build --verbose',
    
              'echo "### Adding built files to git index ###"',
              'cd build/',
              'git add -A',
    
              'echo "### Commiting changes ###"',
              'git commit -m build',
    
              'echo "### Pushing the commit to the gh-pages remote branch ###"',
              'git push origin gh-pages',
              'cd ..'
            ].join(' && '),
            options: {
              stdout: true,
              stderr: true
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2011-08-01
      • 1970-01-01
      • 2013-02-14
      • 2013-12-10
      • 2015-05-06
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      相关资源
      最近更新 更多