【问题标题】:Guard is being inconsistent with guard-sass, guard-haml and guard-coffeeGuard 与guard-sass、guard-haml 和guard-coffee 不一致
【发布时间】:2023-03-22 09:25:01
【问题描述】:

我想为我的大多数项目使用一个配置。它在我的项目文件夹中搜索任何 sass、haml 或 coffee 文件夹。当我添加一个新的地方来制作 sass 文件时,我不想更新配置。

起始文件夹结构

 - sass
     - test.sass
 - coffee
     - test.coffee
 - haml
     - test.html.haml
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - coffee
                     - test2.coffee
                 - haml
                     - test2.html.haml
 - Guardfile

想要生成的文件和文件夹的文件夹结构:

 - sass
     - test.sass
 - css
     - test.css
 - coffee
     - test.coffee
 - js
     - test.js
 - haml
     - test.html.haml
 - test.html
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - css
                     - test2.css
                 - coffee
                     - test2.coffee
                 - js
                     - test2.js
                 - haml
                     - test2.html.haml
                 - test2.html
 - Guardfile

到目前为止,haml 可以完美地与

guard :haml, input: 'haml' do
  watch(/^.+(\.html\.haml)$/)
end

sass,coffeescript 在完全相同的代码下工作方式不同

guard :coffeescript, input: 'coffee', output: 'js' do 
  watch(/^.+(\.coffee)$/) 
end 

guard :sass, input: 'sass', output: 'css' do
  watch(/^.+(\.sass)$/)
end

最终结果导致将目标 css 和 js 文件夹放置在项目文件夹的根目录中。即使我删除了output: 属性,它也会在根目录下创建一个coffee 和sass 文件夹。

结果文件夹结构

 - sass
     - test.sass
 - css
     - test.css
     - test2.css
 - coffee
     - test.coffee
 - js
     - test.js
     - test2.js
 - haml
     - test.html.haml
 - test.html
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - coffee
                     - test2.coffee
                 - haml
                     - test2.html.haml
                 - test2.html
 - Guardfile

我不知道发生了什么,有人可以告诉我吗?

使用 Guard 版本 2.6.1

【问题讨论】:

    标签: coffeescript sass haml guard


    【解决方案1】:

    每个插件对如何解释/使用目录都有自己的想法。

    在coffeescript中,有一个“技巧”告诉它输出目录,方法是将输入目录部分放在一个正则表达式组中,例如:

    guard :coffeescript, input: 'coffee', output: 'js' do
      watch(/^(.+)\\.coffee$/)
    end
    

    但我猜这会创建例如js/a/folder/somewhere/deep,而你想要a/folder/somewhere/deep/js

    如果您不指定输出目录,您可能会得到:a/folder/somewhere/deep - 很接近,但末尾没有 /js/ 文件夹。

    事实上,Haml 文件不同,因为虽然您希望 (...)coffee/test2.coffee(...)/js/test2.js 结尾,但您希望 (...)/haml/test2.html.haml(...)/test2.html.haml 结尾,而不是 (...)/html/test2.html.haml(意思是:你不不想把它放在html 子文件夹中——我同意这很愚蠢)......

    ...这意味着guard-haml 是您对其他插件的期望中的例外。

    所以短篇小说是:

    你不能指望 guard-coffeescriptguard-sass 表现得像 guard-haml, because at the same time you're expecting them to *not* behave the same way (nohtmloutput folder for.haml` 文件 - 这显然是可以理解的)。

    在 haml 中, 像你想要的那样工作,因为你没有为它提供 :output 选项(试试看会发生什么)。

    同时,guard-coffeescript 已(由我)更新以使用较新版本的警卫,因此模板现在有点不同,您可能需要:

    input_dir = 'a/folder/somewhere/deep'
    
    coffeescript_options = {
      input: "#{input_dir}/coffee,
      output: "#{input_dir}/js",
      patterns: [%r{^#{input_dir}/(.+\.(?:coffee|coffee\.md|litcoffee))$}]
    }
    
    guard 'coffeescript', coffeescript_options do
      coffeescript_options[:patterns].each { |pattern| watch(pattern) }
    end
    

    guard-sass 尚未针对较新版本的 Guard 进行更新(事实上,它从未迁移到 Guard 2.0),所以我什至不会费心去调查。

    我看到的选择是:

    • 更改guard-coffeescript 以执行您想要的操作(反转输入/输出文件夹顺序)

    • 重新组织你的结构(可能建议更快)

    由于 Guardfile 是纯 ruby​​,您可以选择迭代 - 如果有帮助,例如:

    # I don't know which parts of the structure are projects, so I'm guessing it's the first part
    Dir[*].each do |project|
      next unless File.directory?(project)
      next if %w(sass css coffee js).include?(project) #(skip top level non-project folders)
      input_dir = "#{project}/a/folder/somewhere/deep"
    
      coffeescript_options = {
        input: "#{input_dir}/coffee,
        output: "#{input_dir}/js",
        patterns: [%r{^#{input_dir}/(.+\.(?:coffee|coffee\.md|litcoffee))$}]
      }
    
      guard 'coffeescript', coffeescript_options do
        coffeescript_options[:patterns].each { |pattern| watch(pattern) }
      end
    
      # guard 'sass' would go here with a similar setup
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-31
      • 2016-09-07
      • 2011-12-11
      • 1970-01-01
      • 2011-05-10
      • 2017-07-16
      • 2012-12-24
      • 2023-03-21
      相关资源
      最近更新 更多