Skip to content

Commit 71e0317

Browse files
committed
Cleanup
1 parent 76b58e5 commit 71e0317

File tree

9 files changed

+302
-11
lines changed

9 files changed

+302
-11
lines changed

grails-scaffolding/grails-app/commands/scaffolding/CreateScaffoldControllerCommand.groovy

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import org.grails.io.support.Resource
3232
* Creates a scaffolded controller.
3333
* Usage: <code>./gradlew runCommand "-Pargs=create-scaffold-controller [DOMAIN_CLASS_NAME]"</code>
3434
*
35-
* @author Puneet Behl
3635
* @since 5.0.0
3736
*/
3837
@CompileStatic
@@ -56,11 +55,25 @@ class CreateScaffoldControllerCommand implements GrailsApplicationCommand, Comma
5655
}
5756
boolean overwrite = isFlagPresent('force')
5857
final Model model = model(sourceClass)
58+
59+
String namespace = flag('namespace')
60+
boolean useService = isFlagPresent('service')
61+
62+
Map<String, Object> templateModel = model.asMap()
63+
templateModel.put('useService', useService)
64+
templateModel.put('namespace', namespace ?: '')
65+
66+
String destinationPath = "grails-app/controllers/${model.packagePath}"
67+
68+
if (namespace) {
69+
destinationPath = "${destinationPath}/${namespace}"
70+
}
71+
5972
render(template: template('scaffolding/ScaffoldedController.groovy'),
60-
destination: file("grails-app/controllers/${model.packagePath}/${model.convention('Controller')}.groovy"),
61-
model: model,
73+
destination: file("${destinationPath}/${model.convention('Controller')}.groovy"),
74+
model: templateModel,
6275
overwrite: overwrite)
63-
verbose('Scaffold controller created for class domain-class')
76+
verbose('Scaffold controller created for domain class')
6477

6578
return SUCCESS
6679
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package scaffolding
20+
21+
import groovy.transform.CompileStatic
22+
23+
import grails.build.logging.ConsoleLogger
24+
import grails.build.logging.GrailsConsole
25+
import grails.codegen.model.Model
26+
import grails.dev.commands.GrailsApplicationCommand
27+
import grails.plugin.scaffolding.CommandLineHelper
28+
import grails.plugin.scaffolding.SkipBootstrap
29+
import org.grails.io.support.Resource
30+
31+
/**
32+
* Creates a scaffolded service.
33+
* Usage: <code>./gradlew runCommand "-Pargs=create-scaffold-service [DOMAIN_CLASS_NAME]"</code>
34+
*
35+
* @author Scott Murphy Heiberg
36+
* @since 7.1.0
37+
*/
38+
@CompileStatic
39+
class CreateScaffoldServiceCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap {
40+
41+
String description = 'Creates a scaffolded service'
42+
43+
@Delegate
44+
ConsoleLogger consoleLogger = GrailsConsole.getInstance()
45+
46+
boolean handle() {
47+
final String domainClassName = args[0]
48+
if (!domainClassName) {
49+
error('No domain-class specified')
50+
return FAILURE
51+
}
52+
final Resource sourceClass = source(domainClassName)
53+
if (!sourceClass) {
54+
error("No domain-class found for name: ${domainClassName}")
55+
return FAILURE
56+
}
57+
boolean overwrite = isFlagPresent('force')
58+
final Model model = model(sourceClass)
59+
render(template: template('scaffolding/ScaffoldedService.groovy'),
60+
destination: file("grails-app/services/${model.packagePath}/${model.convention('Service')}.groovy"),
61+
model: model,
62+
overwrite: overwrite)
63+
verbose('Scaffold service created for domain class')
64+
65+
return SUCCESS
66+
}
67+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package scaffolding
20+
21+
import groovy.transform.CompileStatic
22+
23+
import grails.build.logging.ConsoleLogger
24+
import grails.build.logging.GrailsConsole
25+
import grails.codegen.model.Model
26+
import grails.dev.commands.GrailsApplicationCommand
27+
import grails.plugin.scaffolding.CommandLineHelper
28+
import grails.plugin.scaffolding.SkipBootstrap
29+
import org.grails.io.support.Resource
30+
31+
/**
32+
* Generates a scaffolded service and controller.
33+
* Usage: <code>./gradlew runCommand "-Pargs=generate-scaffold-all [DOMAIN_CLASS_NAME]"</code>
34+
*
35+
* @author Scott Murphy Heiberg
36+
* @since 7.1.0
37+
*/
38+
@CompileStatic
39+
class GenerateScaffoldAllCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap {
40+
41+
String description = 'Generates a scaffolded service and controller'
42+
43+
@Delegate
44+
ConsoleLogger consoleLogger = GrailsConsole.getInstance()
45+
46+
boolean handle() {
47+
final String domainClassName = args[0]
48+
if (!domainClassName) {
49+
error('No domain-class specified')
50+
return FAILURE
51+
}
52+
final Resource sourceClass = source(domainClassName)
53+
if (!sourceClass) {
54+
error("No domain-class found for name: ${domainClassName}")
55+
return FAILURE
56+
}
57+
boolean overwrite = isFlagPresent('force')
58+
final Model model = model(sourceClass)
59+
60+
String namespace = flag('namespace')
61+
62+
// Generate scaffolded service
63+
render(template: template('scaffolding/ScaffoldedService.groovy'),
64+
destination: file("grails-app/services/${model.packagePath}/${model.convention('Service')}.groovy"),
65+
model: model,
66+
overwrite: overwrite)
67+
verbose('Scaffold service created for domain class')
68+
69+
// Generate scaffolded controller with service reference
70+
Map<String, Object> templateModel = model.asMap()
71+
templateModel.put('useService', true)
72+
templateModel.put('namespace', namespace ?: '')
73+
74+
String controllerDestinationPath = "grails-app/controllers/${model.packagePath}"
75+
76+
if (namespace) {
77+
controllerDestinationPath = "${controllerDestinationPath}/${namespace}"
78+
}
79+
80+
render(template: template('scaffolding/ScaffoldedController.groovy'),
81+
destination: file("${controllerDestinationPath}/${model.convention('Controller')}.groovy"),
82+
model: templateModel,
83+
overwrite: overwrite)
84+
verbose('Scaffold controller created for domain class with service reference')
85+
86+
return SUCCESS
87+
}
88+
}

grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/CommandLineHelper.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ trait CommandLineHelper {
3939
}
4040
}
4141

42+
String flag(String name) {
43+
final CommandLine commandLine = executionContext.commandLine
44+
if (commandLine.hasOption(name)) {
45+
return commandLine.optionValue(name)?.toString()
46+
} else {
47+
return commandLine?.undeclaredOptions?.get(name)?.toString()
48+
}
49+
}
50+
4251
}

grails-scaffolding/src/main/scripts/CreateScaffoldController.groovy

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,28 @@ description("Creates a scaffolded controller") {
2222
completer org.grails.cli.interactive.completers.DomainClassCompleter
2323
argument name:'Controller Name', description:"The name of controller", required:true
2424
flag name:'force', description:"Whether to overwrite existing files"
25+
flag name:'namespace', description:"The namespace for the controller"
26+
flag name:'service', description:"Use RestfulServiceController instead of RestfulController as a base class"
2527
}
2628

27-
def model = model(args[0])
29+
def modelInstance = model(args[0])
2830
def overwrite = flag('force') ? true : false
31+
def namespace = flag('namespace')
32+
def useService = flag('service') ? true : false
33+
34+
def templateModel = modelInstance.asMap()
35+
templateModel.put('useService', useService)
36+
templateModel.put('namespace', namespace ?: '')
37+
38+
def destinationPath = "grails-app/controllers/${modelInstance.packagePath}"
39+
40+
if (namespace) {
41+
destinationPath = "${destinationPath}/${namespace}"
42+
}
2943

3044
render template: template('scaffolding/ScaffoldedController.groovy'),
31-
destination: file("grails-app/controllers/${model.packagePath}/${model.convention("Controller")}.groovy"),
32-
model: model,
45+
destination: file("${destinationPath}/${modelInstance.convention("Controller")}.groovy"),
46+
model: templateModel,
3347
overwrite: overwrite
3448

3549
return true
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
description("Creates a scaffolded service") {
21+
usage 'create-service [service name]'
22+
completer org.grails.cli.interactive.completers.DomainClassCompleter
23+
argument name:'Service Name', description:"The name of service", required:true
24+
flag name:'force', description:"Whether to overwrite existing files"
25+
}
26+
27+
def modelInstance = model(args[0])
28+
def overwrite = flag('force') ? true : false
29+
30+
render template: template('scaffolding/ScaffoldedService.groovy'),
31+
destination: file("grails-app/services/${modelInstance.packagePath}/${modelInstance.convention("Service")}.groovy"),
32+
model: modelInstance,
33+
overwrite: overwrite
34+
35+
return true
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
description("Generates a scaffolded service and controller") {
21+
usage 'generate-scaffold-all [domain class name]'
22+
completer org.grails.cli.interactive.completers.DomainClassCompleter
23+
argument name:'Domain Class Name', description:"The name of domain class", required:true
24+
flag name:'force', description:"Whether to overwrite existing files"
25+
flag name:'namespace', description:"The namespace for the controller"
26+
}
27+
28+
def modelInstance = model(args[0])
29+
def overwrite = flag('force') ? true : false
30+
def namespace = flag('namespace')
31+
32+
// Generate scaffolded service
33+
render template: template('scaffolding/ScaffoldedService.groovy'),
34+
destination: file("grails-app/services/${modelInstance.packagePath}/${modelInstance.convention("Service")}.groovy"),
35+
model: modelInstance,
36+
overwrite: overwrite
37+
38+
// Generate scaffolded controller with service reference
39+
def templateModel = modelInstance.asMap()
40+
templateModel.put('useService', true)
41+
templateModel.put('namespace', namespace ?: '')
42+
43+
def controllerDestinationPath = "grails-app/controllers/${modelInstance.packagePath}"
44+
45+
if (namespace) {
46+
controllerDestinationPath = "${controllerDestinationPath}/${namespace}"
47+
}
48+
49+
render template: template('scaffolding/ScaffoldedController.groovy'),
50+
destination: file("${controllerDestinationPath}/${modelInstance.convention("Controller")}.groovy"),
51+
model: templateModel,
52+
overwrite: overwrite
53+
54+
return true
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<%=packageName ? "package ${packageName}" : ''%>
1+
<% if (namespace) { %><%=packageName ? "package ${packageName}.${namespace}" : "package ${namespace}"%>
22

3-
class ${className}Controller {
3+
import ${packageName}.${className}<% } else { %><%=packageName ? "package ${packageName}" : ''%><% } %>
44

5-
static scaffold = ${className}
5+
import grails.plugin.scaffolding.annotation.Scaffold<% if (useService) { %>
6+
import grails.plugin.scaffolding.RestfulServiceController<% } %>
67

7-
}
8+
<% if (useService) { %>@Scaffold(RestfulServiceController<${className}>)<% } else { %>@Scaffold(domain = ${className})<% } %>
9+
class ${className}Controller {<% if (namespace) { %>
10+
static namespace = '${namespace}'
11+
<% } %>}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<%=packageName ? "package ${packageName}" : ''%>
2+
3+
import grails.plugin.scaffolding.annotation.Scaffold
4+
5+
@Scaffold(domain = ${className})
6+
class ${className}Service {
7+
}

0 commit comments

Comments
 (0)