Skip to content

Commit e9c63fe

Browse files
committed
Create a new class for commands to extend from that includes utilities that scripts do
1 parent 0e5a226 commit e9c63fe

File tree

7 files changed

+856
-0
lines changed

7 files changed

+856
-0
lines changed

grails-bootstrap/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import org.apache.tools.ant.filters.ReplaceTokens
22

33
dependencies {
44
compile ( "org.codehaus.groovy:groovy-xml:$groovyVersion" )
5+
compile ( "org.codehaus.groovy:groovy-templates:$groovyVersion" )
56
compile "org.yaml:snakeyaml:1.14"
67

78
compileOnly("org.fusesource.jansi:jansi:$jansiVersion")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package grails.dev.commands
2+
3+
import grails.codegen.model.ModelBuilder
4+
import grails.dev.commands.io.FileSystemInteraction
5+
import grails.dev.commands.io.FileSystemInteractionImpl
6+
import grails.dev.commands.template.TemplateRenderer
7+
import grails.dev.commands.template.TemplateRendererImpl
8+
9+
10+
trait GrailsApplicationCommand implements ApplicationCommand, ModelBuilder {
11+
12+
@Delegate TemplateRenderer templateRenderer
13+
@Delegate FileSystemInteraction fileSystemInteraction
14+
ExecutionContext executionContext
15+
16+
boolean handle(ExecutionContext executionContext) {
17+
this.executionContext = executionContext
18+
this.templateRenderer = new TemplateRendererImpl(executionContext.baseDir)
19+
this.fileSystemInteraction = new FileSystemInteractionImpl(executionContext.baseDir)
20+
handle()
21+
}
22+
23+
List<String> getArgs() {
24+
executionContext.commandLine.remainingArgs
25+
}
26+
27+
abstract boolean handle()
28+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2014 original authors
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
9+
*
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.
15+
*/
16+
package grails.dev.commands.io
17+
18+
import org.grails.io.support.Resource
19+
20+
21+
/**
22+
* Utility methods exposed to scripts for interacting with resources (found on the file system or jars) and the file system
23+
*
24+
* @author Graeme Rocher
25+
* @since 3.0
26+
*/
27+
interface FileSystemInteraction {
28+
29+
/**
30+
* Makes a directory
31+
*
32+
* @param path The path to the directory
33+
*/
34+
FileSystemInteraction mkdir(path)
35+
/**
36+
* Deletes a file
37+
*
38+
* @param path The path to the file
39+
*/
40+
FileSystemInteraction delete(path)
41+
/**
42+
* Allows Gradle style simple copy specs
43+
*
44+
* @param callable The callable
45+
* @return this
46+
*/
47+
FileSystemInteraction copy(@DelegatesTo(CopySpec) Closure callable)
48+
/**
49+
* Copies a resource to the target destination
50+
*
51+
* @param path The path
52+
* @param destination The destination
53+
*/
54+
FileSystemInteraction copy(path, destination)
55+
/**
56+
* Copies resources to the target destination
57+
*
58+
* @param path The path
59+
* @param destination The destination
60+
*/
61+
FileSystemInteraction copyAll(Iterable resources, destination)
62+
/**
63+
* Copy a Resource from the given location to the given directory or location
64+
*
65+
* @param from The resource to copy
66+
* @param to The location to copy to
67+
* @return The {@FileSystemInteraction} instance
68+
*/
69+
FileSystemInteraction copy(Resource from, File to)
70+
/**
71+
* Obtain a file for the given path
72+
*
73+
* @param path The path
74+
* @return The file
75+
*/
76+
File file(Object path)
77+
/**
78+
* @return The target build directory
79+
*/
80+
File getBuildDir()
81+
/**
82+
* @return The directory where resources are processed to
83+
*/
84+
File getResourcesDir()
85+
/**
86+
* @return The directory where classes are compiled to
87+
*/
88+
File getClassesDir()
89+
/**
90+
* Finds a source file for the given class name
91+
* @param className The class name
92+
* @return The source resource
93+
*/
94+
Resource source(String className)
95+
/**
96+
* Obtain a resource for the given path
97+
* @param path The path
98+
* @return The resource
99+
*/
100+
Resource resource(Object path)
101+
/**
102+
* Obtain resources for the given pattern
103+
*
104+
* @param pattern The pattern
105+
* @return The resources
106+
*/
107+
Collection<Resource> resources(String pattern)
108+
/**
109+
* Obtain the path of the resource relative to the current project
110+
*
111+
* @param path The path to inspect
112+
* @return The relative path
113+
*/
114+
String projectPath(Object path)
115+
116+
/**
117+
* The class name of the given resource
118+
*
119+
* @param resource The resource
120+
* @return The class name
121+
*/
122+
String className(Resource resource)
123+
124+
/**
125+
* Get files matching the given pattern
126+
*
127+
* @param pattern The pattern
128+
* @return the files
129+
*/
130+
Collection<File> files(String pattern)
131+
132+
static class CopySpec {
133+
def from
134+
def into
135+
void from(path) {
136+
this.from = path
137+
}
138+
void into(path) {
139+
this.into = path
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)