Skip to content

Commit

Permalink
Merge pull request #636 from moqui/createComponent
Browse files Browse the repository at this point in the history
Add start component to addons and createComponent task to build.grade
  • Loading branch information
acetousk authored Jul 18, 2024
2 parents 1c69844 + e4e6663 commit 5edbd03
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

<!-- Example Component -->
<component name="example" group="moqui" version="3.0.0" branch="master"/>
<component name="start" group="moqui" version="1.0.0" branch="master"/>

<!-- Moqui Tool Components -->
<component name="moqui-aws" group="moqui" version="1.1.0" branch="master"/>
Expand Down
179 changes: 179 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,185 @@ task getComponent {
getComponentTop(curLocationType)
}
}
task createComponent {
description "Create a new component. Set new component name with -Pcomponent=new_component_name (based on the moqui start component here: https://github.com/moqui/start)"
doLast {
String curLocationType = file('.git').exists() ? 'git' : 'current'
if (project.hasProperty('locationType')) curLocationType = locationType

if (project.hasProperty('component')) {
checkRuntimeDirAndDefaults(curLocationType)
Set compsChecked = new TreeSet()

def startComponentName = 'start'

File componentDir = getComponent(startComponentName, curLocationType, parseAddons(), parseMyaddons(), compsChecked)
if (componentDir?.exists()) {
logger.lifecycle("Got component start, dependent components checked: ${compsChecked}")

def newComponent = file("runtime/component/${component}")
def renameSuccessful = componentDir.renameTo(newComponent)
if (!renameSuccessful) {
logger.error("Failed to rename component start to ${component}. Try removing the existing component directory first or giving this program write permissions.")
} else {
logger.lifecycle("Renamed component start to ${component}")
}

print "Updated file: "
newComponent.eachFileRecurse(groovy.io.FileType.FILES) { file ->
try {
// If file name is startComponentName.* rename to component.*
if (file.name.startsWith(startComponentName)) {
String newFileName = (file.name - startComponentName)
newFileName = component + newFileName
File newFile = new File(file.parent, newFileName)
file.renameTo(newFile)
file = newFile
print "${file.path - newComponent.path - '/'}, "
}

String content = file.text
if (content.contains(startComponentName)) {
content = content.replaceAll(startComponentName, component)
file.text = content
print "${file.path - newComponent.path - '/'}, "
}
} catch (IOException e) {
println "Error processing file ${file.path}: ${e.message}"
}
}
print "\n\n"
println "Select rest api (r), screens (s), or both (B):"
def componentInput = System.in.newReader().readLine()

if (componentInput == 'r') {
new File(newComponent, 'screen').deleteDir()
new File(newComponent, 'template').deleteDir()
new File(newComponent, 'data/AppSeedData.xml').delete()
new File(newComponent, 'MoquiConf.xml').delete()
def moquiConf = new File(newComponent, 'MoquiConf.xml')
moquiConf.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<!-- No copyright or license for configuration file, details here are not considered a creative work. -->\n" +
"<moqui-conf xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://moqui.org/xsd/moqui-conf-3.xsd\">\n" +
"</moqui-conf>")
println "Selected rest api so, deleted screen, template, and AppSeedData.xml\n"
} else if (componentInput == 's') {
new File(newComponent, "services/${component}.rest.xml").delete()
new File(newComponent, 'data/ApiSeedData.xml').delete()
println "Selected screens so, deleted rest api and ApiSeedData.xml\n"
} else if (componentInput == 'b' || componentInput == 'B' || componentInput == '') {
println "Selected both rest api and screens\n"
} else {
println "Invalid input. Try again"
newComponent.deleteDir()
return
}

println "Are you going to code or test in groovy or java [y/N]"
def codeInput = System.in.newReader().readLine()

if (codeInput == 'y' || codeInput == 'Y') {
println "Keeping src folder\n"
} else if (codeInput == 'n' || codeInput == 'N' || codeInput == '') {
new File(newComponent, 'src').deleteDir()
new File(newComponent, 'build.grade').delete()
println "Selected no so, deleted src and build.grade\n"
} else {
println "Invalid input. Try again"
newComponent.deleteDir()
return
}

println "Setup a git repository [Y/n]"
def gitInput = System.in.newReader().readLine()
if (gitInput == 'y' || gitInput == 'Y' || gitInput == '') {
new File(newComponent, '.git').deleteDir()
// Setup git repository

def grgit = Grgit.init(dir: newComponent.path)
grgit.add(patterns: ['.'])
// Can't get signing to work easily. If signing works well then might as well commit
// grgit.commit(message: 'Initial commit')
println "Selected yes, so git is initialized\n"
println "To setup the git remote origin, type the git remote url or enter to skip"
def remoteUrl = System.in.newReader().readLine()
if (remoteUrl != '') {
grgit.remote.add(name: 'origin', url: remoteUrl)
println "Run the following to push the git repository:\ncd runtime/component/${component} && git commit -m 'Initial commit' && git push && cd ../../.."
} else {
println "Run the following to push the git repository:\ncd runtime/component/${component} && git commit -m 'Initial commit' && git remote add origin [email protected]:yourgroup/${component} && git push && cd ../../.."
}
} else if (gitInput == 'n' || gitInput == 'N') {
new File(newComponent, '.git').deleteDir()
println "Selected no, so git is not initialized\n"
println "Run the following to push the git repository:\ncd runtime/component/${component} && git commit -m 'Initial commit' && git remote add origin [email protected]:yourgroup/${component} && git push && cd ../../.."
} else {
println "Invalid input. Try again"
newComponent.deleteDir()
return
}

println "Add to myaddons.xml [Y/n]"
def myaddonsInput = System.in.newReader().readLine()
if (myaddonsInput == 'y' || myaddonsInput == 'Y' || myaddonsInput == '') {
def myaddonsFile = file('myaddons.xml')
if (myaddonsFile.exists()){
// Iterate through myaddons file and delete the lines that are </addons>
// Read the lines from the file
def lines = myaddonsFile.readLines()

// Filter out the lines that contain </addons>
def filteredLines = lines.findAll { !it.contains("</addons>") }

// Write the filtered lines back to the file
myaddonsFile.text = filteredLines.join('\n')
} else {
println "myaddons.xml not found. Creating one\nEnter repository github (g), github-ssh (GS), bitbucket (b), or bitbucket-ssh (bs)"
def repositoryInput = System.in.newReader().readLine()
myaddonsFile.append("<addons default-repository=\"")
if (repositoryInput == 'g' || repositoryInput == 'G') {
myaddonsFile.append('github')
} else if (repositoryInput == 'gs' || repositoryInput == 'GS' || repositoryInput == '') {
myaddonsFile.append('github-ssh')
} else if (repositoryInput == 'b' || repositoryInput == 'B') {
myaddonsFile.append('bitbucket')
} else if (repositoryInput == 'bs' || repositoryInput == 'BS') {
myaddonsFile.append('bitbucket-ssh')
} else {
println "Invalid input. Setting to github-ssh"
myaddonsFile.append('github-ssh')
}
myaddonsFile.append("\">")
}

println "Enter the component git repository group"
def groupInput = System.in.newReader().readLine()

println "Enter the component git repository name"
def nameInput = System.in.newReader().readLine()

// get git branch
def grgit = Grgit.open(dir: newComponent.path)
def branch = grgit.branch.current().name

myaddonsFile.append("\n <component group=\"${groupInput}\" name=\"${nameInput}\" branch=\"${branch}\"/>")
myaddonsFile.append("\n</addons>")

} else if (myaddonsInput == 'n' || myaddonsInput == 'N') {
println "Selected no, so component not added to myaddons.xml\n"
} else {
println "Invalid input. Try again"
newComponent.deleteDir()
return
}

}
} else {
throw new InvalidUserDataException("No component property specified")
}

}
}
task getCurrent {
description "Get the current archive for a component, also check each component it depends on and if not present get its current archive; requires component property"
doLast { getComponentTop('current') }
Expand Down

0 comments on commit 5edbd03

Please sign in to comment.