Skip to content
This repository was archived by the owner on Mar 26, 2018. It is now read-only.

Modified move/copy behaviour to enable a user choice. #27

Closed
wants to merge 3 commits into from

Conversation

loicortola
Copy link

In my build flow, I came with the following issue:
I specified a dest folder, but still needed to move the resources (instead of making a copy)

This behaviour is now implemented in a few lines, in a non regressive way. A user now simply needs to override the keepOriginalFiles option to false if he wants to use this feature.

@loicortola
Copy link
Author

Test passed with success, would you consider integrating this pull request within the actual module?

@eddiemonge
Copy link
Member

Why wouldn't you just provide a dest?

@loicortola
Copy link
Author

Simply because my dest is the same as my cwd.

files: {
        expand: true,
        cwd: 'dist',
        dest: 'dist',
        // Note: index.html and admin.html must not be prefixed.
        src: ['**/*.html', '!{index,admin}.html', 'images/*', 'scripts/{,controllers/,services/}*.js', 'styles/*.css', 'i18n/*.json'],
        filter: 'isFile'
      }

@eddiemonge
Copy link
Member

How do you handle things like version control? Why are they the same directory? I am curious about different workflows.

@loicortola
Copy link
Author

###Project looks like this:
app/i18n/ //i18n resources
app/scripts/ //js
app/scripts/controllers //angularjs controllers
app/scripts/services //angularjs services
app/styles/ //css, fonts etc...
app/styles/sass/ //scss files
app/views/ //html views
app/index.html

###Build cycle

clean dist/*
copy static content to dist/ except for scripts and sass
compile sass files and output in dist/styles folder
minify css files in dist/styles folder (move)
minify i18n resources in dist/i18n folder (move)
uglify and minify js resources from app/styles to dist/styles folder
add revisions to files in dist/ (filerev, ideally moving instead of copying)
update file names (usemin) in dist/
copy assets (bower_components etc...)

I am curious and open to suggestions if you have any better idea in terms of workflow.
If I didn't have the ability to move instead of copy in filerev, I would have to get an extra temporary folder, and use another copy task...

Thanks for your input

@eddiemonge
Copy link
Member

is there merely to have minified and unminified, built files in the same directory?

@loicortola
Copy link
Author

Yes basically, in the styles folder for instance, we will have some css files moved from the original directory, plus the compiled css from sass. Now that both of them are in the same directory, we will add revisions to all of them. my workflow basically moves the resources one by one so they can get computed globally with whatever is in the dist folder at one instant. It will only work if I move resources, because today it copies them and I have two versions of each file in dist (one minified, the other not)

@eddiemonge
Copy link
Member

now I am totally confused by what you are trying to accomplish

@loicortola
Copy link
Author

I guess there's no other way than sharing the Gruntfile, it will be easier.
As I said, I am open to suggestions if you think of a different easier workflow, I admit that mine doesn't give me full satisfaction.

    //Cleaning task
    clean: {
      all: ['dist', '.tmp'],
      tmp: ['.tmp']
      webapp: [
        '../trainout-webservices/src/main/webapp/bower_components',
        '../trainout-webservices/src/main/webapp/i18n',
        '../trainout-webservices/src/main/webapp/scripts',
        '../trainout-webservices/src/main/webapp/styles',
        '../trainout-webservices/src/main/webapp/views',
        '../trainout-webservices/src/main/webapp/*.html'
      ]
    },

    //Copy task
    copy: {
      dist: {
        expand: true,
        cwd: "app",
        src: [
          '**',
          '!styles/sass/**',
          '!bower_components/**',
          '!scripts/**/*.js',
          '[**, !bower_components]/*.html',
          'images/*'
        ],
        dest: "dist"
      },
      assets: {
        expand: true,
        src: ['bower_components/**','bower.json'],
        dest: "dist"
      },
      debug: {
        cwd: "app",
        expand: true,
        src: ['**', '!styles/sass/**'],
        dest: "dist"
      }
    },

    //SCSS Compile task
    sass: {
      files: // Put compiled css in .tmp/styles
      {
        expand: true,
        cwd: "app/styles/sass",
        src: "**/*.scss",
        dest: ".tmp/styles",
        ext: '.css'
      }
    },

    //CSS Minify task
    cssmin: {
      files: // Minify css to dist/ destination
      {
        expand: true,
        cwd: ".tmp/styles",
        src: "**/*.css",
        dest: "dist/styles"
      }
    },

    //JSON Minify task
    'json-minify': {
      build: {
        files: 'dist/i18n/**/*.json'
      }
    },

    //Javascript minify task
    uglify: {
      dist: {
        expand: true,
        cwd: 'app',
        src: ['scripts/**/*.js'],
        dest: 'dist'
      }
    },

    //Add revision prefix
    filerev: {
      options: {
        encoding: 'utf8',
        algorithm: 'md5',
        length: 8,
        keepOriginalFiles: false
      },
      files: {
        expand: true,
        cwd: 'dist',
        dest: 'dist',
        // Note: index.html and admin.html must not be prefixed.
        src: ['**/*.html', '!{index,admin}.html', 'images/*',
          'scripts/{,controllers/,services/}*.js', 'styles/*.css', 'i18n/*.json'],
        filter: 'isFile'
      }
    },

    //Update old files matches to new ones
    usemin: {
      html: 'dist/**/*.html',
      css: 'dist/**/*.css',
      // Custom matchings
      htmlextra: 'dist/**/*.html',
      js: 'dist/scripts/{,controllers/,services/}*.js',
      options: {
        patterns: {
          htmlextra: [
            [/src="'([^"']+)'"/gm, 
             "Update the HTML angular includes with new HTML filenames"]
          ],
          js: [
            [/['"]([^"']+)["']/gm,
             'Update the JS with the new HTML filenames']
          ]
        }
      }
    },

    //Bower install task
    bowerInstall: {
      target: {
        // Files updated when running bower install
        src: [
          '**/*.html'   // .html support...
        ],
        cwd: "dist"
      }
    },

    //Connect task
    connect: {
      options: {
        port: 9000,
        livereload: 32323,
        hostname: 'localhost'
      },
      livereload: {
        options: {
          open: 'localhost',
          base: ['dist']
        }
      }
    },

    //Watch task
    watch: {
      app: {
          files: ['app/**'],
          tasks: ['default'],
          options: {
              reload: true
          }
      }
    }

  });

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-filerev');
  grunt.loadNpmTasks('grunt-usemin');
  grunt.loadNpmTasks('grunt-json-minify');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-bower-install');

  // Default task.
  grunt.registerTask('default', ['clean:all', 'copy:dist', 'sass',
         'cssmin', 'json-minify', 'uglify', 'filerev', 'usemin', 'copy:assets',
         'bowerInstall', 'clean:tmp']);

  // Serve task.
  grunt.registerTask('serve', function () {
    grunt.task.run(['default', 'connect', 'watch']);
  });

@mwillerich
Copy link

isn't #21 a simpler way to achieve the same?

@loicortola
Copy link
Author

Exactly the same feature, I like the "copy" option better, however the algorithm he used could still be improved a little.
Also needs to update README.md.
Merging any of those will be fine with me :)

@mwillerich
Copy link

It's nearly the same feature: in #21 the choice whether files are copied or moved is entirely controlled by the flag (if the flag is set to copy: 1, it is always copied; with copy: 0 it's never copied), in this PR (unless I'm misreading it), you can't make a copy and not specify a dest.

@loicortola
Copy link
Author

Yes, you are absolutely right :)

@eddiemonge
Copy link
Member

I'm going to close this in favor of #21. @loicortola if you feel that is missing something, please ask for it there or suggest fixes. Thanks though

@eddiemonge eddiemonge closed this May 15, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants