All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marklogic.gradle.task.NewProjectTask.groovy Maven / Gradle / Ivy

/*
 * Copyright (c) 2023 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.gradle.task

import com.marklogic.appdeployer.scaffold.ScaffoldGenerator
import org.gradle.api.tasks.TaskAction

class NewProjectTask extends MarkLogicTask {

	@TaskAction
	void newProject() {
		println "\nWelcome to the new project wizard. Please respond to each of the following prompts to start a new project.\n"
		println "Each prompt below begins with '[ant:input]'; type your response for each prompt on the blank line, or press 'Enter' to " +
			"accept the default value or values in the brackets.\n"
		println "Note that this will overwrite your current build.gradle and gradle.properties files, and backup copies of each will be made.\n"
		ant.input(message: "Application name:", addproperty: "mlAppName", defaultvalue: "myApp")
		ant.input(message: "Host to deploy to:", addproperty: "mlHost", defaultvalue: "localhost")
		ant.input(message: "MarkLogic admin username:", addproperty: "mlUsername", defaultvalue: "admin")
		ant.input(message: "MarkLogic admin password:", addproperty: "mlPassword", defaultvalue: "admin")
		ant.input(message: "REST API port (leave blank for no REST API server):", addproperty: "mlRestPort")
		if (ant.mlRestPort) {
			ant.input(message: "Test REST API port (intended for running automated tests; leave blank for no server):", addproperty: "mlTestRestPort")
		}
		ant.input(message: "Do you want support for multiple environments? ", validargs: "y,n", addproperty: "mlPropertiesPlugin", defaultvalue: "y")
		ant.input(message: "Do you want a set of users/roles created?", validargs: "y,n", addproperty: "mlScaffoldSecurity", defaultvalue: "y")

		def now = new Date()

		def propertiesText = "# Properties generated by mlNewProject at ${now}" +
			"\n# See https://github.com/marklogic/ml-gradle/wiki/Property-reference for a list of all supported properties" +
			"\nmlAppName=${ant.mlAppName}" +
			"\nmlHost=${ant.mlHost}" +
			"\nmlUsername=${ant.mlUsername}" +
			"\nmlPassword=${ant.mlPassword}"

		if (ant.mlRestPort) {
			propertiesText += "\nmlRestPort=${ant.mlRestPort}"
			if (ant.mlTestRestPort) {
				propertiesText += "\nmlTestRestPort=${ant.mlTestRestPort}"
			}
		} else {
			propertiesText += "\nmlNoRestServer=true"
		}

		if (ant.mlPropertiesPlugin == "y") {
			def text = 'plugins {' +
				'\n  id "net.saliman.properties" version "1.5.2"' +
				'\n  id "com.marklogic.ml-gradle" version "5.0.0"' +
				'\n}'
			println "Updating build.gradle so that the Gradle properties plugin can be applied"
			writeFile("build.gradle", text)
			propertiesText += "\n\n# Controls which additional properties file is used by the Gradle properties plugin - https://github.com/stevesaliman/gradle-properties-plugin"
			propertiesText += "\n# Defaults to 'local' when the property doesn't exist, and gradle-local.properties should be ignored by version control"
			propertiesText += "\n# environmentName="
		}

		writeFile("gradle.properties", propertiesText)

		if (ant.mlPropertiesPlugin == "y") {
			writeFile("gradle-dev.properties", "# Generated by mlNewProject at " + now)
			writeFile("gradle-local.properties", "# Generated by mlNewProject at " + now + "\n# Please be sure to ignore this file in version control!")
			writeFile("gradle-qa.properties", "# Generated by mlNewProject at " + now)
			writeFile("gradle-prod.properties", "# Generated by mlNewProject at " + now)
		}

		makeDirectory("src/main/ml-config")
		makeDirectory("src/main/ml-modules")

		var scaffoldSecurity = (ant.mlScaffoldSecurity == "y")
		var scaffoldRestServers = false
		if (ant.mlRestPort) {
			scaffoldRestServers = true
		}
		ScaffoldGenerator.AppInputs appInputs = new ScaffoldGenerator.AppInputs(ant.mlAppName, scaffoldRestServers, scaffoldSecurity)
		new ScaffoldGenerator().generateScaffold(getProject().getProjectDir().getAbsolutePath(), appInputs)
	}

	void makeDirectory(String path) {
		File f = new File(getProject().getProjectDir(), path);
		f.mkdirs()
		println "Making directory: " + f.getAbsolutePath()
	}

	void writeFile(String filename, String text) {
		File file = new File(getProject().getProjectDir(), filename);
		if (file.exists()) {
			new File("backup-" + filename).write(file.text)
		}
		println "Writing: " + file.getAbsolutePath()
		file.write(text)
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy