1- /**
2- This class provides utility functions helpful for continuous automated release.
1+ /*
2+ Copyright 2014-2019 Sam Gleske - https://github.com/samrocketman/jervis
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
39
4- @author Sam Gleske (GitHub @samrocketman)
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
515 */
616
717package net.gleske.jervis.tools
@@ -12,7 +22,51 @@ import groovy.text.SimpleTemplateEngine
1222import java.util.regex.Pattern
1323
1424/**
15- This class provides automatic release utility functions such as pattern matching
25+ This utility class provides automatic release methods such as pattern
26+ matching and version bumping if given existing releases.
27+
28+ <h2>Sample usage</h2>
29+ <p >To run this example, clone Jervis and execute <tt >./gradlew console</tt>
30+ to bring up a <a href =" http://groovy-lang.org/groovyconsole.html" target =" _blank" >Groovy Console</a> with the classpath set up.</p>
31+
32+ <pre ><tt >import static net.gleske.jervis.tools.AutoRelease.getScriptFromTemplate
33+ import static net.gleske.jervis.tools.AutoRelease.getNextRelease
34+ import static net.gleske.jervis.tools.AutoRelease.getNextSemanticRelease
35+ import static net.gleske.jervis.tools.AutoRelease.isMatched
36+
37+ String template = '''
38+ Say hello to the ${type}: <% print kinds.join(', ') %><% kinds.each { String kind -> %>
39+ Hello ${kind}!<% } %>
40+ '''.trim()
41+ println "Show a string built from a simple Groovy template:"
42+ println getScriptFromTemplate(template, [type: 'animals', kinds: ['dog', 'cat', 'snake']])
43+ println getScriptFromTemplate(template, [type: 'people', kinds: ['sam', 'kristie', 'tammy']])
44+ println()
45+
46+ String date = new Date().format('YYMMdd')
47+ List<String> git_tags = ['v1.1', 'v1.2', '1.1.2-1']
48+
49+ println 'the first release for a date-based version comparing already released git tags:'
50+ println getNextRelease(date, git_tags)
51+ println()
52+
53+ println 'the 3rd release for a version whose git tags start with the prefix "v"; hotfix releases keep a "." as the separator:'
54+ println getNextRelease('1.0', git_tags, '.', 'v')
55+ println()
56+
57+ println 'get the next semantic versioning hotfix release when a hotfix for 1.1.2 has already been released as git tags:'
58+ println getNextSemanticRelease('1.1.2', git_tags)
59+ println()
60+
61+ println('='*80)
62+ println 'check some version numbers to see if they are a valid semantic versioning format'
63+ println()
64+ ['1.0', '1.0.1-3', '1.2.1', '1.2.1-beta', '1.0.0-rc', '1.0-beta'].each { String version ->
65+ println "${version} version matches semantic versioning?"
66+ println isMatched('/([0-9]+\\.){2}[0-9]+(-.*)?$/', version)
67+ println()
68+ }
69+ println('='*80)</pre></tt>
1670 */
1771class AutoRelease {
1872
@@ -163,9 +217,11 @@ class AutoRelease {
163217 }
164218
165219 /**
166- Get bumps the version with a more loosely formed format. This
220+ Gets a bumped version with a more loosely formed format. This
167221 automatic releasing method is similar to semantic versioning but it is
168- not as strict.
222+ not as strict. Passed in version numbers can be anything. For more
223+ strict semantic versioning see
224+ <tt ><a href =" #getNextSemanticRelease(java.lang.String, List<String>, java.lang.String)" >getNextSemanticRelease</a></tt>.
169225
170226 <h2>For Java, Python, and other languages</h2>
171227 <tt >-SNAPSHOT</tt> is automatically stripped from <tt >version</tt> as part of getting the next semantic version release.
@@ -286,11 +342,85 @@ class AutoRelease {
286342 complicated shell scripting such as using regex and needing to substitute
287343 Groovy variables.
288344
345+ An example of <tt >script</tt> and <tt >variables</tt> used by this
346+ method would be the following.
347+
348+ <pre ><tt >import static net.gleske.jervis.tools.AutoRelease.getScriptFromTemplate
349+
350+ String script = '''
351+ #!/bin/bash
352+ set -euxo pipefail
353+
354+ <% tools.each { Map tool -> %>if [ -f "${tool.markerFile}" ]; then
355+ echo "This project contains code for the build tool ${tool.name}."
356+ fi
357+ <% } %>
358+ '''.trim()
359+
360+ Map variables = [
361+ tools: [
362+ [
363+ name: 'maven',
364+ markerFile: 'pom.xml'
365+ ],
366+ [
367+ name: 'gradle',
368+ markerFile: 'build.gradle'
369+ ],
370+ [
371+ name: 'NodeJS npm',
372+ markerFile: 'package.json'
373+ ],
374+ [
375+ name: 'NodeJS yarn',
376+ markerFile: 'yarn.lock'
377+ ],
378+ [
379+ name: 'Python setuptools',
380+ markerFile: 'setup.py'
381+ ]
382+ ]
383+ ]
384+ println 'Build tool detection script'
385+ println('='*80)
386+ println getScriptFromTemplate(script, variables)
387+ println('='*80)</tt></pre>
388+
389+ Which returns the following output.
390+
391+ <pre ><tt >Build tool detection script
392+ ================================================================================
393+ #!/bin/bash
394+ set -euxo pipefail
395+
396+ if [ -f "pom.xml" ]; then
397+ echo "This project contains code for the build tool maven."
398+ fi
399+ if [ -f "build.gradle" ]; then
400+ echo "This project contains code for the build tool gradle."
401+ fi
402+ if [ -f "package.json" ]; then
403+ echo "This project contains code for the build tool NodeJS npm."
404+ fi
405+ if [ -f "yarn.lock" ]; then
406+ echo "This project contains code for the build tool NodeJS yarn."
407+ fi
408+ if [ -f "setup.py" ]; then
409+ echo "This project contains code for the build tool Python setuptools."
410+ fi
411+
412+ ================================================================================</tt></pre>
413+
414+ This example shows that you can pass in Groovy templates with bindings.
415+ By using this method yourself you do not need to manage the template
416+ engine or binding. Just pass in the template, bindings, and use them
417+ through this method.
418+
289419 @param script A shell script which is a Groovy template meant to
290420 contain variables to be replaced.
291421 @param variables A Map of variables to replace in the script.
292422 */
293- static String getScriptFromTemplate (String script , Map< String , String > variables ) {
423+ static String getScriptFromTemplate (String script , Map variables ) {
294424 SimpleTemplateEngine engine = new SimpleTemplateEngine ()
295425 engine. createTemplate(script). make(variables). toString()
296426 }
0 commit comments