2023-08-18 07:52:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-18 16:02:00 +00:00
|
|
|
"errors"
|
2023-08-18 07:52:58 +00:00
|
|
|
"fmt"
|
2023-08-18 09:40:46 +00:00
|
|
|
"io"
|
2023-08-18 07:52:58 +00:00
|
|
|
"log"
|
|
|
|
|
"os"
|
2023-08-18 11:36:52 +00:00
|
|
|
"os/exec"
|
2023-08-18 16:02:00 +00:00
|
|
|
"path/filepath"
|
2023-08-18 08:39:29 +00:00
|
|
|
"strings"
|
2023-08-18 07:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 09:40:46 +00:00
|
|
|
func copy(src, dst string) (int64, error) {
|
|
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
|
return 0, fmt.Errorf("%s is not a regular file", src)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer destination.Close()
|
|
|
|
|
nBytes, err := io.Copy(destination, source)
|
|
|
|
|
return nBytes, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 07:52:58 +00:00
|
|
|
func main() {
|
2023-08-31 20:59:47 +00:00
|
|
|
var ver string = "1.3"
|
|
|
|
|
var version int = 4
|
2023-08-18 16:02:00 +00:00
|
|
|
|
|
|
|
|
argsar := os.Args[1:]
|
|
|
|
|
var debugelement string = "-d"
|
|
|
|
|
var quickelement string = "-q"
|
2023-08-30 23:53:39 +00:00
|
|
|
var buildelement string = "--buildversion"
|
2023-08-31 20:59:47 +00:00
|
|
|
var addfavelement string = "-f"
|
2023-08-18 16:02:00 +00:00
|
|
|
var debugargument bool = false
|
|
|
|
|
var quickargument bool = false
|
2023-08-30 23:53:39 +00:00
|
|
|
var buildargument bool = false
|
2023-08-31 20:59:47 +00:00
|
|
|
var addfavargument bool = false
|
2023-08-18 16:02:00 +00:00
|
|
|
|
|
|
|
|
for i := 0; i < len(argsar); i++ {
|
|
|
|
|
// checking if the array contains the given value
|
|
|
|
|
if argsar[i] == debugelement {
|
|
|
|
|
// changing the boolean variable
|
|
|
|
|
debugargument = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(argsar); i++ {
|
|
|
|
|
// checking if the array contains the given value
|
|
|
|
|
if argsar[i] == quickelement {
|
|
|
|
|
// changing the boolean variable
|
|
|
|
|
quickargument = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:53:39 +00:00
|
|
|
for i := 0; i < len(argsar); i++ {
|
|
|
|
|
// checking if the array contains the given value
|
|
|
|
|
if argsar[i] == buildelement {
|
|
|
|
|
// changing the boolean variable
|
|
|
|
|
buildargument = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 20:59:47 +00:00
|
|
|
for i := 0; i < len(argsar); i++ {
|
|
|
|
|
// checking if the array contains the given value
|
|
|
|
|
if argsar[i] == addfavelement {
|
|
|
|
|
// changing the boolean variable
|
|
|
|
|
addfavargument = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:53:39 +00:00
|
|
|
if buildargument == true {
|
|
|
|
|
fmt.Print(ver)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 07:52:58 +00:00
|
|
|
var projname string
|
|
|
|
|
|
|
|
|
|
fmt.Println("地基")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println("diji " + ver)
|
2023-08-18 07:52:58 +00:00
|
|
|
fmt.Println("by KZacharski")
|
|
|
|
|
fmt.Print("Project name: ")
|
|
|
|
|
fmt.Scanln(&projname)
|
2023-08-18 09:40:46 +00:00
|
|
|
var createcss bool = true
|
|
|
|
|
var createjs bool = true
|
2023-08-18 11:36:52 +00:00
|
|
|
var createsample bool = false
|
|
|
|
|
var initgit bool = true
|
|
|
|
|
var creategitignore bool = true
|
2023-08-18 16:02:00 +00:00
|
|
|
|
|
|
|
|
var configlocation string
|
2023-08-20 20:34:13 +00:00
|
|
|
var configfile string
|
2023-08-18 16:02:00 +00:00
|
|
|
var exPath string
|
2023-08-20 20:34:13 +00:00
|
|
|
if _, err := os.Stat("diji-config"); err == nil {
|
|
|
|
|
configlocation = "diji-config"
|
2023-08-18 16:02:00 +00:00
|
|
|
|
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
ex, err := os.Executable()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
exPath = filepath.Dir(ex)
|
2023-08-20 20:34:13 +00:00
|
|
|
configlocation = exPath + "/diji-config"
|
2023-08-18 16:02:00 +00:00
|
|
|
|
|
|
|
|
}
|
2023-08-20 20:34:13 +00:00
|
|
|
configfile = configlocation + "/config.txt"
|
|
|
|
|
configbytes, err := os.ReadFile(configfile)
|
2023-08-18 09:40:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Print(err)
|
|
|
|
|
}
|
|
|
|
|
configtext := string(configbytes)
|
|
|
|
|
var quickmode bool = strings.Contains(configtext, "quick-mode = true")
|
2023-08-18 10:08:31 +00:00
|
|
|
var debug bool = strings.Contains(configtext, "debug = true")
|
2023-08-31 20:59:47 +00:00
|
|
|
var addfav bool = strings.Contains(configtext, "favicon = true")
|
2023-08-18 10:08:31 +00:00
|
|
|
|
2023-08-18 16:02:00 +00:00
|
|
|
if debugargument == true {
|
|
|
|
|
if debug == true {
|
|
|
|
|
debug = false
|
|
|
|
|
} else if debug == false {
|
|
|
|
|
debug = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if quickargument == true {
|
|
|
|
|
if quickmode == true {
|
|
|
|
|
quickmode = false
|
|
|
|
|
} else if quickmode == false {
|
|
|
|
|
quickmode = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 20:59:47 +00:00
|
|
|
if addfavargument == true {
|
|
|
|
|
if addfav == true {
|
|
|
|
|
addfav = false
|
|
|
|
|
} else if addfav == false {
|
|
|
|
|
addfav = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 10:08:31 +00:00
|
|
|
var cssstr string
|
|
|
|
|
var jsstr string
|
2023-08-18 11:36:52 +00:00
|
|
|
var samplestr string
|
|
|
|
|
var langstr string = "en"
|
|
|
|
|
var gitstr string
|
|
|
|
|
var gitignorestr string
|
|
|
|
|
var gifiles string
|
2023-08-18 09:40:46 +00:00
|
|
|
|
|
|
|
|
if quickmode == false {
|
2023-08-18 10:08:31 +00:00
|
|
|
|
2023-08-18 09:40:46 +00:00
|
|
|
fmt.Print("Create a css file (y/n, default y): ")
|
|
|
|
|
fmt.Scanln(&cssstr)
|
|
|
|
|
fmt.Print("Create a js file (y/n, default y): ")
|
|
|
|
|
fmt.Scanln(&jsstr)
|
2023-08-18 11:36:52 +00:00
|
|
|
fmt.Print("Website language (default en): ")
|
|
|
|
|
fmt.Scanln(&langstr)
|
|
|
|
|
fmt.Print("Insert sample content (y/n, default n): ")
|
|
|
|
|
fmt.Scanln(&samplestr)
|
|
|
|
|
fmt.Print("Initialize a git repo (y/n, default y): ")
|
|
|
|
|
fmt.Scanln(&gitstr)
|
2023-08-18 16:02:00 +00:00
|
|
|
if gitstr == "n" {
|
|
|
|
|
initgit = false
|
|
|
|
|
}
|
|
|
|
|
if initgit == true {
|
|
|
|
|
fmt.Print("Create .gitignore (y/n, default y): ")
|
|
|
|
|
fmt.Scanln(&gitignorestr)
|
|
|
|
|
if gitignorestr == "n" {
|
|
|
|
|
creategitignore = false
|
|
|
|
|
}
|
|
|
|
|
if creategitignore == true {
|
|
|
|
|
fmt.Print("Add files/file types to .gitignore: ")
|
|
|
|
|
fmt.Scanln(&gifiles)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-18 09:40:46 +00:00
|
|
|
if cssstr == "n" {
|
|
|
|
|
createcss = false
|
|
|
|
|
}
|
|
|
|
|
if jsstr == "n" {
|
|
|
|
|
createjs = false
|
|
|
|
|
}
|
2023-08-18 11:36:52 +00:00
|
|
|
if samplestr == "y" {
|
|
|
|
|
createsample = true
|
|
|
|
|
}
|
2023-08-18 16:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if initgit == false {
|
|
|
|
|
creategitignore = false
|
2023-08-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-18 07:52:58 +00:00
|
|
|
if err := os.Mkdir(projname, os.ModePerm); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var indname string = projname + "/index.html"
|
|
|
|
|
index, err := os.Create(indname)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2023-08-31 20:59:47 +00:00
|
|
|
|
|
|
|
|
if addfav == true {
|
|
|
|
|
var assetspath string = projname + "/assets"
|
|
|
|
|
if err := os.Mkdir(assetspath, os.ModePerm); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var favpath string = assetspath + "/favicon.png"
|
|
|
|
|
copy(configlocation+"/defaultfav.png", favpath)
|
|
|
|
|
fmt.Println(favpath + " created.")
|
2023-08-18 09:40:46 +00:00
|
|
|
}
|
2023-08-31 20:59:47 +00:00
|
|
|
|
2023-08-18 09:40:46 +00:00
|
|
|
var indexcontent1 string = `<!DOCTYPE html>
|
2023-08-18 11:36:52 +00:00
|
|
|
<html lang="` + langstr + `">
|
2023-08-18 08:39:29 +00:00
|
|
|
<head>
|
2023-08-18 11:36:52 +00:00
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
2023-08-18 08:39:29 +00:00
|
|
|
<title>` + projname + `</title>
|
2023-08-18 09:40:46 +00:00
|
|
|
`
|
|
|
|
|
var csstag string = `<link rel="stylesheet" type="text/css" href="style.css">
|
|
|
|
|
`
|
|
|
|
|
var jstag string = `<script src="script.js"></script>
|
|
|
|
|
`
|
|
|
|
|
var indexcontent2 string = `</head>
|
2023-08-18 11:36:52 +00:00
|
|
|
<body>
|
|
|
|
|
</body>
|
2023-08-18 09:40:46 +00:00
|
|
|
</html>`
|
2023-08-18 11:36:52 +00:00
|
|
|
var samplecontent string = `<h1>` + projname + `</h1>
|
2023-08-18 16:25:35 +00:00
|
|
|
<h3>Generated with diji ` + ver + `</h3>
|
2023-08-18 11:36:52 +00:00
|
|
|
<p>Website content</p>`
|
|
|
|
|
if createsample == true {
|
|
|
|
|
indexcontent2 = `</head>
|
|
|
|
|
<body>
|
|
|
|
|
` + samplecontent + `
|
|
|
|
|
</body>`
|
|
|
|
|
}
|
2023-08-31 20:59:47 +00:00
|
|
|
var favtag string = `<link rel="icon" type="image" href="./assets/favicon.png">
|
|
|
|
|
`
|
|
|
|
|
if addfav == true {
|
|
|
|
|
indexcontent1 = indexcontent1 + favtag
|
|
|
|
|
}
|
2023-08-18 09:40:46 +00:00
|
|
|
if createcss == true {
|
|
|
|
|
indexcontent1 = indexcontent1 + csstag
|
|
|
|
|
}
|
|
|
|
|
if createjs == true {
|
|
|
|
|
indexcontent1 = indexcontent1 + jstag
|
|
|
|
|
}
|
|
|
|
|
var indexcontent string = indexcontent1 + indexcontent2
|
2023-08-18 08:39:29 +00:00
|
|
|
_, err2 := index.WriteString(indexcontent)
|
2023-08-18 07:52:58 +00:00
|
|
|
check(err2)
|
|
|
|
|
defer index.Close()
|
|
|
|
|
fmt.Println(indname + " created.")
|
2023-08-18 09:40:46 +00:00
|
|
|
|
2023-08-18 10:08:31 +00:00
|
|
|
var cssname string
|
|
|
|
|
var csscontent string
|
|
|
|
|
|
2023-08-18 09:40:46 +00:00
|
|
|
if createcss == true {
|
2023-08-18 10:08:31 +00:00
|
|
|
cssname = projname + "/style.css"
|
|
|
|
|
csscontent = `body {
|
2023-08-18 09:40:46 +00:00
|
|
|
font-family: sans-serif;
|
|
|
|
|
}`
|
|
|
|
|
style, err := os.Create(cssname)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
_, err2 := style.WriteString(csscontent)
|
|
|
|
|
check(err2)
|
|
|
|
|
defer style.Close()
|
|
|
|
|
fmt.Println(cssname + " created.")
|
2023-08-18 08:39:29 +00:00
|
|
|
}
|
2023-08-18 10:08:31 +00:00
|
|
|
|
|
|
|
|
var jsname string
|
2023-08-18 09:40:46 +00:00
|
|
|
if createjs == true {
|
2023-08-18 10:08:31 +00:00
|
|
|
jsname = projname + "/script.js"
|
2023-08-18 09:40:46 +00:00
|
|
|
script, err := os.Create(jsname)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer script.Close()
|
|
|
|
|
fmt.Println(jsname + " created.")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 11:36:52 +00:00
|
|
|
var giname string
|
|
|
|
|
var gicontent string
|
|
|
|
|
|
|
|
|
|
if creategitignore == true {
|
|
|
|
|
giname = projname + "/.gitignore"
|
|
|
|
|
gicontent = ".DS_Store " + gifiles
|
|
|
|
|
gignore, err := os.Create(giname)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
_, err2 := gignore.WriteString(gicontent)
|
|
|
|
|
check(err2)
|
|
|
|
|
defer gignore.Close()
|
|
|
|
|
|
|
|
|
|
fmt.Println(giname + " created.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if initgit == true {
|
|
|
|
|
cmd := exec.Command("git", "init", projname)
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
cmd1 := exec.Command("git", "add", ".")
|
|
|
|
|
cmd1.Dir = "./" + projname
|
|
|
|
|
if err := cmd1.Run(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd2 := exec.Command("git", "commit", "-a", "-m", `"Initial commit"`)
|
|
|
|
|
cmd2.Dir = "./" + projname
|
|
|
|
|
if err := cmd2.Run(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Git repo initialized.")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 10:08:31 +00:00
|
|
|
if debug == true {
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(version)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Println("projname(string): " + projname)
|
|
|
|
|
fmt.Print("createcss(bool): ")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(createcss)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Print("createjs(bool): ")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(createjs)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Print("configbytes([]byte]): ")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(configbytes)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Println("configtext(string): " + configtext)
|
|
|
|
|
fmt.Print("quickmode(bool): ")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(quickmode)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Print("debug(bool): ")
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println(debug)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Println("cssstr(string): " + cssstr)
|
|
|
|
|
fmt.Println("jsstr(string): " + jsstr)
|
|
|
|
|
fmt.Println("indname(string): " + indname)
|
2023-08-31 20:59:47 +00:00
|
|
|
//fmt.Println("assetspath(string): " + assetspath)
|
|
|
|
|
// fmt.Println("favpath(string): " + favpath)
|
2023-08-18 10:08:31 +00:00
|
|
|
fmt.Println("indexcontent1(string): " + indexcontent1)
|
|
|
|
|
fmt.Println("csstag(string): " + csstag)
|
|
|
|
|
fmt.Println("jstag(string): " + jstag)
|
|
|
|
|
fmt.Println("indexcontent2(string): " + indexcontent2)
|
|
|
|
|
fmt.Println("indexcontent(string): " + indexcontent)
|
|
|
|
|
fmt.Println("cssname(string): " + cssname)
|
|
|
|
|
fmt.Println("csscontent(string): " + csscontent)
|
|
|
|
|
fmt.Println("jsname(string): " + jsname)
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Print("argsar(array): ")
|
|
|
|
|
fmt.Println(argsar)
|
|
|
|
|
fmt.Println("debugelement(string): " + debugelement)
|
|
|
|
|
fmt.Println("quickelement(string): " + quickelement)
|
|
|
|
|
fmt.Print("debugargument(bool): ")
|
|
|
|
|
fmt.Println(debugargument)
|
|
|
|
|
fmt.Print("quickargument(bool): ")
|
|
|
|
|
fmt.Println(quickargument)
|
|
|
|
|
fmt.Println("configlocation(string): " + configlocation)
|
2023-08-20 20:34:13 +00:00
|
|
|
fmt.Println("configfile(string): " + configfile)
|
2023-08-18 16:02:00 +00:00
|
|
|
fmt.Println("exPath(string): " + exPath)
|
|
|
|
|
fmt.Println("samplestr(string): " + samplestr)
|
|
|
|
|
fmt.Println("langstr(string): " + langstr)
|
|
|
|
|
fmt.Println("gitstr(string): " + gitstr)
|
|
|
|
|
fmt.Println("gitignorestr(string): " + gitignorestr)
|
|
|
|
|
fmt.Println("gifiles(string): " + gifiles)
|
|
|
|
|
fmt.Print("createsample(bool): ")
|
|
|
|
|
fmt.Println(createsample)
|
|
|
|
|
fmt.Print("initgit(bool): ")
|
|
|
|
|
fmt.Println(initgit)
|
|
|
|
|
fmt.Print("creategitignore(bool): ")
|
|
|
|
|
fmt.Println(creategitignore)
|
|
|
|
|
fmt.Println("samplecontent(string)" + samplecontent)
|
|
|
|
|
fmt.Println("giname(string)" + giname)
|
|
|
|
|
fmt.Println("gicontent(string)" + gicontent)
|
2023-08-18 10:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-18 11:36:52 +00:00
|
|
|
fmt.Println("Project created in ./" + projname + ".")
|
|
|
|
|
fmt.Println("Thanks for using diji.")
|
|
|
|
|
|
2023-08-18 07:52:58 +00:00
|
|
|
}
|