Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleman: run custom action after build

Tags:

ruby

middleman

How can I run custom action (eg. copy file to build folder) after middleman built pages?

I want to put Readme.md file from source to build dir.

like image 420
NARKOZ Avatar asked May 26 '14 23:05

NARKOZ


2 Answers

You can use after_build hook. add following code to config.rb.

The hook which you can use is written in https://middlemanapp.com/advanced/custom_extensions/ .

Although it is not well documented, it seems that after_build can use directly in config.rb, without writing your own extension.

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
like image 148
ymonad Avatar answered Sep 27 '22 21:09

ymonad


Though the after_build hook is a default answer, i would suggest using a task runner to do the job.

A task runner is awesome to make such routines easier. For example, most Middleman projects require deployment to a hosting server. So if you happen to use a task runner for deployment, you could use it for copying the file as well.

If you don't use a task runner, consider using one. It will save you a lot of nuisance.

Rake is the natural choice for Middleman's Ruby environment, but i prefer Grunt.

Here's a Grunt copy task (uses the grunt-contrib-copy plugin):

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

And here's a deployment task using the grunt-shell plugin:

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
        }
    }

}
like image 28
Andrey Mikhaylov - lolmaus Avatar answered Sep 27 '22 21:09

Andrey Mikhaylov - lolmaus