Skip to content

Commit 8cfdfb1

Browse files
authored
Merge pull request #10164 from grails/GRAILS-10032
Add support for commands in applications
2 parents cc05f66 + 88e1245 commit 8cfdfb1

File tree

11 files changed

+868
-6
lines changed

11 files changed

+868
-6
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")

grails-core/src/main/groovy/grails/dev/commands/ApplicationCommand.groovy

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ package grails.dev.commands
1818
import grails.util.Described
1919
import grails.util.GrailsNameUtils
2020
import grails.util.Named
21-
import org.grails.build.parsing.CommandLine
2221
import org.springframework.context.ConfigurableApplicationContext
2322

24-
25-
2623
/**
2724
* Represents a command that is run against the {@link org.springframework.context.ApplicationContext}
2825
*

grails-core/src/main/groovy/grails/dev/commands/ApplicationContextCommandRegistry.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package grails.dev.commands
1818
import groovy.transform.CompileStatic
1919
import org.grails.core.io.support.GrailsFactoriesLoader
2020

21-
2221
/**
2322
* A registry of {@grails.dev.commands.ApplicationContextCommand} instances
2423
*

grails-core/src/main/groovy/grails/dev/commands/ExecutionContext.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import groovy.transform.Canonical
2020
import groovy.transform.CompileStatic
2121
import org.grails.build.parsing.CommandLine
2222

23-
24-
2523
/**
2624
* A context command to pass to {@link ApplicationCommand} instances
2725
*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
trait GrailsApplicationCommand implements ApplicationCommand, ModelBuilder {
10+
11+
@Delegate TemplateRenderer templateRenderer
12+
@Delegate FileSystemInteraction fileSystemInteraction
13+
ExecutionContext executionContext
14+
15+
boolean handle(ExecutionContext executionContext) {
16+
this.executionContext = executionContext
17+
this.templateRenderer = new TemplateRendererImpl(executionContext.baseDir)
18+
this.fileSystemInteraction = new FileSystemInteractionImpl(executionContext.baseDir)
19+
handle()
20+
}
21+
22+
List<String> getArgs() {
23+
executionContext.commandLine.remainingArgs
24+
}
25+
26+
abstract boolean handle()
27+
}
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)