如何寫 arara 規則來有條件地執行命令

如何寫 arara 規則來有條件地執行命令

我想編寫一個arara規則,根據外部文件的存在有條件地執行命令。

這是我能夠使用的範例規則:

!config
# open rule for arara
# author: AEllett
# requires arara 3.0+
identifier: echo
name: echo
command: /bin/echo "-->@{getBasename(file)}<--"
arguments: []

我還可以有條件地測試文件

!config
# open rule for arara
# author: AEllett
# requires arara 3.0+
identifier: test
name: testing
commands: 
  - <arara> /bin/echo @{ isTrue ( isFile ("./dir/file") , "TRUE" , "FALSE") }
arguments: []

我想改變我正在執行的內容:即“/bin/echo”部分。但是當我嘗試類似的事情時:

!config
# open rule for arara
# author: AEllett
# requires arara 3.0+
identifier: test
name: testing
commands: 
  - <arara> @{mycommand} @{ isTrue ( isFile ("./dir/file") , "TRUE" , ".") }
arguments: 
  - identifier: mycommand
    flag: <arara> @{ isTrue ( isFile ("./dir/file") , "/bin/echo " , "/bin/ls ") }

它運行時沒有錯誤,但沒有執行我想要的操作。

我也嘗試過類似的事情:

!config
# open rule for arara
# author: AEllett
# requires arara 3.0+
identifier: test
name: testing
commands: 
  - <arara> @{mycommand} @{ isTrue ( isFile ("./dir/file") , "TRUE" , ".") }
arguments: 
  - identifier: mycommand
    flag: <arara> @{ isFile ("./dir/file") == true  ?  "/bin/echo " : "/bin/ls " }

這會產生一個錯誤

It appears that the 'test' task has a YAML syntax error or an
invalid field. Could you take a look at the 'test.yaml' file
located at '/Users/acellett/projects/ini/arara/rules'. I tried my
best to dump the error message, so here it is:

Problem: mapping values are not allowed here
Error found in line 12, column 60.
     ... ("./dir/file")  ?  "/bin/echo " : "/bin/ls " }
                                         ^

我的測試檔案(名為「example_01.tex」)如下所示:

%  arara: test: { files: [example_01.tex] }

從我發出的命令列

$ arara -v example_01.tex

日誌記錄的結果是:

01 Jan 2014 10:58:59.870 INFO  Arara - Welcome to arara!
01 Jan 2014 10:58:59.874 INFO  Arara - Processing file 'example_01.tex', please wait.
01 Jan 2014 10:58:59.875 INFO  DirectiveExtractor - Reading directives from example_01.tex.
01 Jan 2014 10:58:59.876 TRACE DirectiveExtractor - Directive found in line 1 with test: { files: [example_01.tex] }.
01 Jan 2014 10:58:59.884 INFO  DirectiveParser - Parsing directives.
01 Jan 2014 10:58:59.889 INFO  TaskDeployer - Deploying tasks into commands.
01 Jan 2014 10:58:59.889 TRACE TaskDeployer - Task 'test' found in '/Users/acellett/projects/ini/arara/rules'.
01 Jan 2014 10:58:59.933 INFO  CommandTrigger - Ready to run commands.
01 Jan 2014 10:58:59.934 INFO  CommandTrigger - Running 'testing'.
01 Jan 2014 10:58:59.934 TRACE CommandTrigger - Command:  TRUE
01 Jan 2014 10:59:00.063 TRACE CommandTrigger - Output logging:
01 Jan 2014 10:59:00.064 TRACE CommandTrigger - 
01 Jan 2014 10:59:00.064 INFO  CommandTrigger - 'testing' was successfully executed.
01 Jan 2014 10:59:00.064 INFO  CommandTrigger - All commands were successfully executed.
01 Jan 2014 10:59:00.065 INFO  Arara - Done.

雖然我不太確定如何解釋這一點。

所以基本上我想知道的是如何編寫一個規則,該規則根據其他文件的存在而以不同的方式執行。

順便說一句,我可以寫:

!config
# open rule for arara
# author: AEllett
# requires arara 3.0+
identifier: test
name: testing
commands: 
  - <arara> @{ isTrue ( isFile ("./dir/file") , "/bin/echo " , "/bin/ls ") } @{ isTrue ( isFile ("./dir/file") , "TRUE" , ".") }
arguments: []

但我真的很想通過參數選擇命令。

答案1

顯然,同儕壓力是有效的。:)我正在看著你,湯姆。:P

Plan B,讓我們看看我是否能獲得 800 票: 我是 Facebook 的安全工程師,這是我的錯。 :)

我可能應該專注於如何優化程式碼,但由於我很著急,我現在想不出直接的方法。:)

所引發的錯誤實際上是命令開頭使用的警告之一<arara>:我們可以保存一些引號,但我們可能會遇到來自 YAML 映射的一些問題。

:字元在 YAML 格式中大量使用,因此它稍後出現在字串中會導致解析器感到困惑,從而出現錯誤。解決方案是依靠好的舊引號(單引號或雙引號)。由於您已經有雙引號,因此請使用單引號:

flag: '@{ isFile ("./dir/file") == true  ?  "/bin/echo " : "/bin/ls " }'

現在應該可以了。嗯,有點。:)

flag僅當參數出現在指令中時才進行評估,否則參數將​​被解析為空字串。為了進行此評估,您mycommand指令中包含。如果您不提供它,則在呼叫它的@{mycommand}主行中將為空。command

如果您希望mycommand無論其在指令中出現與否都進行評估,請使用 fordefault而不是flag。讓我們來看一個例子:

- identifier: engine
  flag: <arara> @{parameters.engine}
  default: pdflatex

假設我想要一個規則,在其中我可以定義我想要使用的引擎,所以我有一個engine論點。第一種情況:

% arara: foo

arara會這樣做:

  1. 事情已經發生了default,所以決定engine對其進行評估。
  2. 指令中沒有engine出現,保持原樣。

最終,engine將會是pdflatex。現在我們來看第二種情況:

% arara: foo: { engine: xelatex }

arara現在的行為如下:

  1. 出現了這樣的default情況,所以決定engine對其進行評估,即pdflatex
  2. engine指令中出現了,評估flag並替換預設值。

現在,engine將會是xelatex:)

經過一些小的修改,

!config
identifier: test
name: testing
commands: 
  - <arara> @{mycommand} @{ isTrue ( isFile ("./dir/file") , "TRUE" , ".") }
arguments: 
  - identifier: mycommand
    default: '@{ isFile ("./dir/file") == true  ?  "/bin/echo " : "/bin/ls " }'

你的規則現在有效了。:)

希望能幫助你!:)

相關內容