github.com/shravanasati/hydra@v1.0.1-0.20240122045627-1082d2ed50d2/src/init.go (about)

     1  /*
     2  The following code is responsible for the init command.
     3  
     4  Author: Shravan Asati
     5  Originally Written: 28 March 2021
     6  Last edited: 8 June 2021
     7  */
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"os"
    14  	"os/exec"
    15  	"strconv"
    16  	"strings"
    17  	"time"
    18  )
    19  
    20  // year returns the current year, used for editing LICENSE file.
    21  func year() string {
    22  	return strconv.Itoa(time.Now().Year())
    23  }
    24  
    25  // handleException handles the exception by printing it and exiting the program.
    26  func handleException(err error) {
    27  	if err != nil {
    28  		fmt.Println("FATAL ERROR: Project initialisation failed! This should never happen. You may want to file an issue at the hydra repository: https://github.com/shravanasati/hydra/issues/new?assignees=&labels=&template=bug_report.md&title=")
    29  		fmt.Println(err)
    30  		os.Exit(-1)
    31  	}
    32  }
    33  
    34  type Initializer struct {
    35  	projectName string
    36  	license     string
    37  	lang        string
    38  }
    39  
    40  
    41  
    42  // makeDir creates a directory.
    43  func makeDir(dirname string) {
    44  	err := os.Mkdir(dirname, os.ModePerm)
    45  	handleException(err)
    46  	cwd, _ := os.Getwd()
    47  	fmt.Printf("\n - Created directory '%v' at %v.", dirname, cwd)
    48  }
    49  
    50  // execute executes a command in the shell.
    51  func execute(base string, command ...string) error {
    52  	cmd := exec.Command(base, command...)
    53  	_, err := cmd.Output()
    54  	if err != nil {
    55  		return err
    56  	}
    57  	return nil
    58  }
    59  
    60  // getGitignore returns the gitignore variable from static.go, corresponding to the provided language.
    61  func getGitignore(language string) string {
    62  	switch language {
    63  	case "python":
    64  		return pythonGitignore
    65  	case "go":
    66  		return goGitignore
    67  	case "c":
    68  		return cGitignore
    69  	case "c++":
    70  		return cppGitignore
    71  	case "ruby":
    72  		return rubyGitignore
    73  	default:
    74  		return fmt.Sprintf("Unknown language: %v.", language)
    75  	}
    76  }
    77  
    78  // manipulateLicense replaces the `:NAME:` and `:YEAR:` values of the license with actual values.
    79  func manipulateLicense(license string) string {
    80  	licenseText := strings.Replace(license, ":YEAR:", year(), 1)
    81  	licenseText = strings.Replace(licenseText, ":NAME:", getConfig("fullName"), 1)
    82  
    83  	return licenseText
    84  }
    85  
    86  // getGitignore returns the license variable from static.go, corresponding to the provided license.
    87  func getLicense(license string) string {
    88  	switch license {
    89  	case "MIT":
    90  		return manipulateLicense(MIT)
    91  	case "BSD":
    92  		return manipulateLicense(BSD)
    93  	case "APACHE":
    94  		return manipulateLicense(APACHE)
    95  	case "EPL":
    96  		return manipulateLicense(EPL)
    97  	case "MPL":
    98  		return manipulateLicense(MPL)
    99  	case "GPL":
   100  		return manipulateLicense(GPL)
   101  	case "UNI":
   102  		return manipulateLicense(UNI)
   103  	default:
   104  		return fmt.Sprintf("Undefined license: %v.", license)
   105  	}
   106  }
   107  
   108  func (init *Initializer) initByJson(file string)  {
   109  
   110  }
   111  
   112  // basicInit makes the README, LICENSE and gitignore files.
   113  func (init *Initializer) basicInit() string {
   114  	fmt.Printf("Initialising project: '%v' in %v.\n", init.projectName, init.lang)
   115  
   116  	makeDir(init.projectName)
   117  	os.Chdir(fmt.Sprintf("./%v", init.projectName))
   118  
   119  	gwd, _ := os.Getwd()
   120  	makeFile("LICENSE", getLicense(init.license))
   121  	makeFile("README.md", fmt.Sprintf("# %v", init.projectName))
   122  	makeFile(".gitignore", getGitignore(init.lang))
   123  	return gwd
   124  }
   125  
   126  // pythonInit is the python project initialisation function.
   127  func (init *Initializer) pythonInit() {
   128  	gwd := init.basicInit()
   129  
   130  	pythonSetup = strings.Replace(pythonSetup, ":PROJECT_NAME:", init.projectName, 2)
   131  	pythonSetup = strings.Replace(pythonSetup, ":LICENSE:", init.license, 1)
   132  	pythonSetup = strings.Replace(pythonSetup, ":GITHUB:", getConfig("githubUsername"), 1)
   133  	pythonSetup = strings.Replace(pythonSetup, ":AUTHOR_NAME:", getConfig("fullName"), 1)
   134  	makeFile("setup.py", pythonSetup)
   135  
   136  	makeDir(init.projectName)
   137  	os.Chdir(fmt.Sprintf("./%v", init.projectName))
   138  	makeFile("__init__.py", "")
   139  	os.Chdir(gwd)
   140  
   141  	makeDir("tests")
   142  	os.Chdir("./tests")
   143  	makeFile("__init__.py", "")
   144  	makeFile(fmt.Sprintf("test_%v.py", init.projectName), "")
   145  	os.Chdir(gwd)
   146  
   147  	e := execute("git", "init")
   148  	if e != nil {
   149  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a git repository.")
   150  	} else {
   151  		fmt.Println("\n - Intialised a Git repository for your project.")
   152  	}
   153  }
   154  
   155  // goInit is the go project initialisation function.
   156  func (init *Initializer) goInit() {
   157  	gwd := init.basicInit()
   158  
   159  	makeDir("src")
   160  	os.Chdir("./src")
   161  	makeFile("main.go", "package main")
   162  	os.Chdir(gwd)
   163  
   164  	makeDir("tests")
   165  	os.Chdir("./tests")
   166  	makeFile(fmt.Sprintf("%v_test.go", init.projectName), "package main")
   167  	os.Chdir(gwd)
   168  
   169  	e := execute("go", "mod", "init", fmt.Sprintf("github.com/%v/%v", getConfig("githubUsername"), init.projectName))
   170  	if e != nil {
   171  		fmt.Println("\n ** Go isn't installed on your system. Cannot enable dependency tracking.")
   172  	} else {
   173  		fmt.Println("\n - Enabled dependency tracking for your Go application.")
   174  	}
   175  	e = execute("git", "init")
   176  	if e != nil {
   177  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a repository.")
   178  	} else {
   179  		fmt.Println(" - Intialised a Git repository for your project.")
   180  	}
   181  }
   182  
   183  // webInit is the web-frontend project initialisation function.
   184  func (init *Initializer) webInit() {
   185  	gwd := init.basicInit()
   186  
   187  	indexContent := strings.Replace(HTMLBoilerplate, ":PROJECT_NAME:", init.projectName, 2)
   188  	indexContent = strings.Replace(indexContent, ":CSS_LINK:", `<link rel="stylesheet" href="./css/style.css">`, 1)
   189  	indexContent = strings.Replace(indexContent, ":SCRIPT_LINK:", `<script src="./js/script.js"> </script>`, 1)
   190  	makeFile("index.html", indexContent)
   191  	makeFile("README.md", fmt.Sprintf("# %v", init.projectName))
   192  
   193  	makeDir("img")
   194  
   195  	makeDir("css")
   196  	os.Chdir("./css")
   197  	makeFile("style.css", cssReset)
   198  	os.Chdir(gwd)
   199  
   200  	makeDir("js")
   201  	os.Chdir("./js")
   202  	makeFile("script.js", "")
   203  	os.Chdir(gwd)
   204  
   205  	e := execute("git", "init")
   206  	if e != nil {
   207  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a repository.")
   208  	} else {
   209  		fmt.Println(" - Intialised a Git repository for your project.")
   210  	}
   211  }
   212  
   213  // flaskInit is the python-flask project initialisation function.
   214  func (init *Initializer) flaskInit() {
   215  	gwd := init.basicInit()
   216  
   217  	makeFile("app.py", flaskBoilerplate)
   218  
   219  	// * making the static dir which contains images, styles and scripts dir
   220  	makeDir("static")
   221  	os.Chdir("./static")
   222  
   223  	makeDir("images")
   224  
   225  	makeDir("scripts")
   226  	os.Chdir("./scripts")
   227  	makeFile("script.js", "")
   228  	os.Chdir("..")
   229  
   230  	makeDir("styles")
   231  	os.Chdir("./styles")
   232  	makeFile("style.css", cssReset)
   233  	os.Chdir(gwd)
   234  
   235  	// * making the templates dir
   236  	makeDir("templates")
   237  	os.Chdir("./templates")
   238  	indexContent := strings.Replace(HTMLBoilerplate, ":PROJECT_NAME:", init.projectName, 2)
   239  	indexContent = strings.Replace(indexContent, ":CSS_LINK:", `<link rel="stylesheet" href="{{ url_for('static', filename='styles/style.css') }}">`, 1)
   240  	indexContent = strings.Replace(indexContent, ":SCRIPT_LINK:", `<script src=" {{ url_for('static', filename='scripts/script.js') }} "> </script>`, 1)
   241  	makeFile("index.html", indexContent)
   242  	os.Chdir(gwd)
   243  
   244  	// * initialising git repository
   245  	e := execute("git", "init")
   246  	if e != nil {
   247  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a git repository.")
   248  	} else {
   249  		fmt.Println("\n - Intialised a Git repository for your project.")
   250  	}
   251  }
   252  
   253  // cInit is the C project initialisation function.
   254  func (init *Initializer) cInit() {
   255  	gwd := init.basicInit()
   256  
   257  	makeFile("Makefile.am", "")
   258  
   259  	makeDir("src")
   260  	os.Chdir("./src")
   261  	makeFile("Makefile.am", "")
   262  	makeFile("main.c", "")
   263  	makeFile("main.h", "")
   264  	os.Chdir(gwd)
   265  
   266  	makeDir("tests")
   267  	os.Chdir("./tests")
   268  	makeFile("Makefile.am", "")
   269  	makeFile(fmt.Sprintf("%v_test.c", init.projectName), "")
   270  	os.Chdir(gwd)
   271  
   272  	makeDir("libs")
   273  	os.Chdir("../libs")
   274  	makeFile("Makefile.am", "")
   275  
   276  	e := execute("git", "init")
   277  	if e != nil {
   278  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a repository.")
   279  	} else {
   280  		fmt.Println(" - Intialised a Git repository for your project.")
   281  	}
   282  }
   283  
   284  // cppInit is the C++ project initialisation function.
   285  func (init *Initializer) cppInit() {
   286  	gwd := init.basicInit()
   287  	makeFile("CMakeLists.txt", "")
   288  
   289  	makeDir("src")
   290  	os.Chdir("./src")
   291  	makeFile("main.cpp", "")
   292  	makeFile("main.h", "")
   293  	os.Chdir(gwd)
   294  
   295  	makeDir("include")
   296  	os.Chdir("./include")
   297  	makeDir(init.projectName)
   298  	os.Chdir(fmt.Sprintf("./%v", init.projectName))
   299  	makeFile("header.h", "")
   300  	os.Chdir(gwd)
   301  
   302  	makeDir("tests")
   303  	os.Chdir("./tests")
   304  	makeFile(fmt.Sprintf("%v_test.cpp", init.projectName), "")
   305  	os.Chdir(gwd)
   306  
   307  	makeDir("libs")
   308  
   309  	e := execute("git", "init")
   310  	if e != nil {
   311  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a repository.")
   312  	} else {
   313  		fmt.Println(" - Intialised a Git repository for your project.")
   314  	}
   315  }
   316  
   317  // rubyInit is the ruby project initialisation function.
   318  func (init *Initializer) rubyInit() {
   319  	gwd := init.basicInit()
   320  
   321  	makeFile("Gemfile", "")
   322  	makeFile("Rakefile", "")
   323  
   324  	gemspecContent = strings.Replace(gemspecContent, ":PROJECT_NAME:", init.projectName, 4)
   325  	gemspecContent = strings.Replace(gemspecContent, ":LICENSE:", init.license, 1)
   326  	gemspecContent = strings.Replace(gemspecContent, ":GITHUB:", getConfig("githubUsername"), 1)
   327  	gemspecContent = strings.Replace(gemspecContent, ":AUTHOR_NAME:", getConfig("fullName"), 1)
   328  	makeFile(fmt.Sprintf("%v.gemspec", init.projectName), gemspecContent)
   329  
   330  	makeDir("bin")
   331  
   332  	makeDir("lib")
   333  	os.Chdir("./lib")
   334  	makeFile(fmt.Sprintf("%v.rb", init.projectName), "")
   335  	os.Chdir(gwd)
   336  
   337  	makeDir("tests")
   338  	os.Chdir("./tests")
   339  	makeFile(fmt.Sprintf("test_%v.rb", init.projectName), "")
   340  	os.Chdir(gwd)
   341  
   342  	e := execute("git", "init")
   343  	if e != nil {
   344  		fmt.Println("\n ** Git isn't installed on your system. Cannot initiate a git repository.")
   345  	} else {
   346  		fmt.Println("\n - Intialised a Git repository for your project.")
   347  	}
   348  }