2013-07-05 69 views
3

我想用在那些目录中的所有文件咕噜字符串替换替换字符串及其子目录咕噜字符串替换:在目录中的所有文件和子目录

例如替换字符串在所有这些文件:

dist/templates_and_modules/templates/template1/template1.php 
dist/templates_and_modules/templates/template2/template2.php 
dist/templates_and_modules/modules/module1.php 
dist/templates_and_modules/modules/module1.php 

我要替换

/*remove->*/ 

/* 

/*<-remove*/ 

*/ 

随着它的工作原理明确定义文件:

strrep: { 
    dist: { 
     files: { 
      '<%= yeoman.dist %>/templates_and_modules/templates/template1/template1.php': 
      '<%= yeoman.dist %>/templates_and_modules/templates/template1/template1.php' 
     }, 
     options: { 
      replacements: [ 
       { 
        pattern: '/*remove->*/', 
        replacement: '/*' 
       }, 

       { 
        pattern: '/*<-remove*/', 
        replacement: '*/' 
       } 
       ] 
      } 
     } 
    } 

但我不能让它使用目录中的所有文件。

回答

6

通过反复试验,我发现,这个工程:

files: { 
    './': 'dist/**/*.*' 
} 

但我不明白,为什么。

+0

而不是提供一个对象,尝试提供一个数组。 ['dist /**/*.*']将查找并替换所有文件,其中'a':'b'将用新的更改写入b。 – lededje

+1

是的,它适用于我。我之前使用'./production/html/':'* .html',它不工作,没有错误,我认为这个插件找不到这个设置的目录。当我把它改成''./':'production/html/*。html''时,没关系。 –

5

您应该可以使用globbing patterns来定义它处理的文件夹文件&。

又见Files section of Configuring Tasks

事情是这样的:

src: [ '<%= yeoman.app %>/templates_and_modules/**/*.php' ] 
    dest: '<%= yeoman.dist %>/templates_and_modules/' 

然而,该插件似乎被设计出来的文件复制到一个新的目标,而不是修改它们就地与您试图去做。这是一个完整的例子,把它们拷贝出来到新的位置:

module.exports = function(grunt) { 
    grunt.initConfig({ 
    'string-replace': { 
     dist: { 
     src: './app/mylibs/**/*.js', 
     dest: './dist/mylibs/', 
     options: { 
      replacements: [{ 
      pattern: 'old', 
      replacement: 'new' 
      }] 
     } 
     } 
    } 
    }); 

    grunt.loadNpmTasks('grunt-string-replace'); 
    grunt.registerTask('default', ['string-replace']); 
}; 
+0

这似乎不起作用。如果有人可以发布一个适用于grunt-string-replace – gang

+0

@gang的实际示例,会很酷吗?现在会发生什么?它没有找到这些文件,或者替换文件在这些文件上无法正常工作?你能发布你的最新配置吗? – explunit

+2

我收到一个错误:警告:无法读取“dist/templates_and_modules /”文件(错误代码:EISDIR)。使用--force继续。 – gang

相关问题