diff options
author | Ivan Komissarov <ABBAPOH@gmail.com> | 2019-07-06 17:13:39 +0200 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2019-07-10 11:43:51 +0000 |
commit | 6785335b3be5be4c388fa8d967d880e19b70244c (patch) | |
tree | ce1135a9ef9174bbfe30e2647943ae463875e112 | |
parent | bcf2849298fa170e0286332414e08637faa60f6a (diff) | |
download | qbs-6785335b3be5be4c388fa8d967d880e19b70244c.tar.gz |
Add a complete example for a Rule in a Product
Change-Id: I4739440cb90f7ef5795f79da053246f8071aa57e
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
-rw-r--r-- | doc/reference/items/language/rule.qdoc | 54 | ||||
-rw-r--r-- | examples/examples.qbs | 3 | ||||
-rw-r--r-- | examples/rule/lorem_ipsum.txt | 4 | ||||
-rw-r--r-- | examples/rule/rule.qbs | 36 |
4 files changed, 65 insertions, 32 deletions
diff --git a/doc/reference/items/language/rule.qdoc b/doc/reference/items/language/rule.qdoc index 3a14c2fa6..62a32caf5 100644 --- a/doc/reference/items/language/rule.qdoc +++ b/doc/reference/items/language/rule.qdoc @@ -45,30 +45,28 @@ The following rule takes text files and replaces Windows-style line endings with their Unix-style counterparts. We will look at it one piece at a time. - \code - Rule { - multiplex: false - \endcode + \quotefromfile ../examples/rule/rule.qbs + + \skipto Rule + \printuntil multiplex: false + A \e {multiplex rule} creates one transformer that takes all input artifacts with the matching input file tag and creates one or more output artifacts. We are setting the respective property to \c false here, indicating that we want to create one transformer per input file. \note This is actually the default, so the above assignment is not required. - \code - inputs: ["txt_input"] - \endcode + + \printuntil inputs: ["txt_input"] + Here we are specifying that our rule is interested in input files that have the tag \c "txt_input". Such files could be source files, in which case you would tag them using a \l{Group}. Or they could in turn get generated by a different rule, in which case that rule would assign the file tag. The files matching the tag will be available in the \l{prepare} script under the name \c inputs (see \l{inputs and outputs}{The inputs and outputs Variables}). - \code - Artifact { - filePath: input.fileName + ".out" - fileTags: ["txt_output"] - } - \endcode + + \printuntil } + Here we are specifying that for every input file, we want to create one output file whose name is the same as the input file, but with an additional extension. Because we are giving a relative path, \QBS will prepend that path by the product's build directory. @@ -82,24 +80,8 @@ \l Artifact items. The set of output artifacts will be available in the prepare script under the name \c outputs (see \l{inputs and outputs}{The inputs and outputs Variables}). - \code - prepare: { - var cmd = new JavaScriptCommand(); - cmd.description = input.fileName + "->" + output.fileName; - cmd.highlight = "codegen"; - cmd.sourceCode = function() { - var file = new TextFile(input.filePath); - var content = file.readAll(); - file.close() - content = content.replace(/\r\n/g, "\n"); - file = new TextFile(output.filePath, TextFile.WriteOnly); - file.write(content); - file.close(); - } - return [cmd]; - } - } - \endcode + \printuntil /^\s{4}\}/ + The prepare script shown above puts everything together by creating the command that does the actual transformation of the file contents, employing the help of the \l{TextFile Service}{TextFile} class. @@ -184,6 +166,16 @@ our executable. \endlist + \section1 A Complete Example + + The following code snippet shows a single \l{Rule} within a \l{Product} and summarizes the + previous sections. + + \note The product's type is set up to the \c "txt_output" file tag to tell \QBS that the + product depends on output artifacts produced by the custom rule. Otherwise the rule would not + be executed. + + \quotefile ../examples/rule/rule.qbs */ /*! diff --git a/examples/examples.qbs b/examples/examples.qbs index abb6d5d9a..ff6e7a191 100644 --- a/examples/examples.qbs +++ b/examples/examples.qbs @@ -65,6 +65,7 @@ Project { "install-bundle/install-bundle.qbs", "protobuf/cpp/addressbook.qbs", "protobuf/objc/addressbook.qbs", - "baremetal/baremetal.qbs" + "baremetal/baremetal.qbs", + "rule/rule.qbs", ] } diff --git a/examples/rule/lorem_ipsum.txt b/examples/rule/lorem_ipsum.txt new file mode 100644 index 000000000..2901fbcee --- /dev/null +++ b/examples/rule/lorem_ipsum.txt @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer accumsan laoreet magna vitae +elementum. Duis semper ex pellentesque nibh ullamcorper lacinia. Suspendisse sed diam magna. +Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In id +maximus turpis, mattis commodo mauris. Sed bibendum accumsan leo. Nulla placerat. diff --git a/examples/rule/rule.qbs b/examples/rule/rule.qbs new file mode 100644 index 000000000..8ec14ee40 --- /dev/null +++ b/examples/rule/rule.qbs @@ -0,0 +1,36 @@ +import qbs.TextFile + +Product { + type: "txt_output" + + Group { + name: "lorem_ipsum" + files: "lorem_ipsum.txt" + fileTags: "txt_input" + } + + //![1] + Rule { + multiplex: false + inputs: ["txt_input"] + Artifact { + filePath: input.fileName + ".out" + fileTags: ["txt_output"] + } + prepare: { + var cmd = new JavaScriptCommand(); + cmd.description = input.fileName + "->" + output.fileName; + cmd.highlight = "codegen"; + cmd.sourceCode = function() { + var file = new TextFile(input.filePath); + var content = file.readAll(); + file.close() + content = content.toUpperCase(); + file = new TextFile(output.filePath, TextFile.WriteOnly); + file.write(content); + file.close(); + } + return [cmd]; + } + } +} |