Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single line support added #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ignore the go binaries
licensor
licensor.yml
LICENSE_HEADER
51 changes: 42 additions & 9 deletions steps/iterate_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/Marvin9/licensor/utils"
)
Expand Down Expand Up @@ -52,8 +53,9 @@ func (m *CommandModel) iterateDirectory(path string) {
// PROCESS FILE
// GENERATE COMMENT PREFIX & POSTFIX BASED ON EXTENSION
commentPrefix, commentPostfix := utils.Comment(ext)
var isSingleLine bool = (commentPostfix == utils.SINGLE_LINE_COMMENTS)

uniqueHeader := append([]byte(commentPrefix), []byte(utils.UniqueIdentifier)...)
uniqueHeader := append([]byte(commentPrefix), []byte(utils.DELIMITER)...)

buffer := make([]byte, 512)
file, err := os.OpenFile(fullpath, os.O_RDWR, os.ModePerm)
Expand All @@ -68,9 +70,11 @@ func (m *CommandModel) iterateDirectory(path string) {
if err != nil {
break
}

//read the file content
content := buffer[:read]
//the length of the file before append
fileContentBeforeAppend := len(fileContent)
//the file content with the content appended
fileContent = append(fileContent, content...)

if licenseAlreadyExist == -1 {
Expand All @@ -90,7 +94,15 @@ func (m *CommandModel) iterateDirectory(path string) {

var endOfComment int
uniqueHeaderLen := len(uniqueHeader)
endOfComment = bytes.Index(fileContent[licenseAlreadyExist+uniqueHeaderLen:], []byte(commentPostfix))
var postfix string
//delimiter as the Index search
if isSingleLine == true{
postfix = commentPrefix + utils.DELIMITER
}else{
postfix = utils.DELIMITER + commentPostfix
}

endOfComment = bytes.Index(fileContent[licenseAlreadyExist+uniqueHeaderLen:], []byte(postfix))
endOfComment += licenseAlreadyExist + uniqueHeaderLen
oldLicenseText := bytes.TrimPrefix(fileContent[licenseAlreadyExist:endOfComment], uniqueHeader)

Expand All @@ -105,10 +117,10 @@ func (m *CommandModel) iterateDirectory(path string) {
}
}

lastIdx := endOfComment + len(commentPostfix) - 1
lastIdx := endOfComment + len(postfix) - 1
// REMOVE EXISTING LICENSE
fileContent = append(fileContent[0:licenseAlreadyExist], fileContent[lastIdx+1:len(fileContent)]...)
fileContent = bytes.TrimPrefix(fileContent, []byte("\n\n"))
fileContent = append(fileContent[0:licenseAlreadyExist], fileContent[lastIdx+1:]...)
fileContent = bytes.TrimPrefix(fileContent, []byte("\n"))
} else if m.RemoveFlag {
file.Close()
continue
Expand All @@ -121,11 +133,11 @@ func (m *CommandModel) iterateDirectory(path string) {
// \u001b[0G => place cursor to 0th position
fmt.Printf("\u001b[2K\033[u\u001b[2K\u001b[0G%v\u001b[0G", fullpath)
}

fileToInjectLicense := file
fileToInjectLicense.Truncate(0)
fileToInjectLicense.Seek(0, 0)

// COMMENT OUT LICENSE TEXT
// ---------------------- template --------------------------
// commentPrefix uniqueIdentifier
Expand All @@ -135,8 +147,29 @@ func (m *CommandModel) iterateDirectory(path string) {
//
// actual code
// -----------------------------------------------------------

//copy the value of the license to a temporary string
var finalString = string(m.LicenseText)
//println(finalString)
if isSingleLine == true {
//add delimeter to the end of the file and comment it out
finalString = finalString + "\n" + commentPrefix + utils.DELIMITER
//add the unique identifier to the start of the license
finalString = commentPrefix + utils.UniqueIdentifier + "\n" + finalString
//replace each newline with newline + comment
finalString = strings.ReplaceAll(finalString, "\n ", "\n"+commentPrefix+" ")
}else{
//add the delimeter to the end of the license
finalString = finalString + "\n" + utils.DELIMITER
//make the unique identifier as part of the string
finalString = utils.UniqueIdentifier + "\n" + finalString


}


if !m.RemoveFlag {
fileToInjectLicense.WriteString(fmt.Sprintf("%v%v\n%v\n%v\n\n", commentPrefix, utils.UniqueIdentifier, string(m.LicenseText), commentPostfix))
fileToInjectLicense.WriteString(fmt.Sprintf("%v%v%v\n", commentPrefix + utils.DELIMITER + "\n", finalString, commentPostfix))
}
fileToInjectLicense.Write(fileContent)
file.Close()
Expand Down
52 changes: 28 additions & 24 deletions utils/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ const commonPostfix = "*/"

// SupportedComments is mapping of language extension with syntax
var SupportedComments = map[string]([2]string){
"h": [2]string{commonPrefix, commonPostfix},
"hpp": [2]string{commonPrefix, commonPostfix},
"html": [2]string{"<!-- ", "-->"},
"js": [2]string{commonPrefix, commonPostfix},
"jsx": [2]string{commonPrefix, commonPostfix},
"tsx": [2]string{commonPrefix, commonPostfix},
"css": [2]string{commonPrefix, commonPostfix},
"py": [2]string{"\"\"\"\n", "\"\"\""},
"java": [2]string{commonPrefix, commonPostfix},
"rb": [2]string{"=begin ", "=end"},
"c": [2]string{commonPrefix, commonPostfix},
"cpp": [2]string{commonPrefix, commonPostfix},
"cs": [2]string{commonPrefix, commonPostfix},
"m": [2]string{commonPrefix, commonPostfix},
"go": [2]string{commonPrefix, commonPostfix},
"swift": [2]string{commonPrefix, commonPostfix},
"clj": [2]string{"(comment ", ")"},
"ts": [2]string{commonPrefix, commonPostfix},
"dart": [2]string{commonPrefix, commonPostfix},
"elm": [2]string{"{- ", "-}"},
"groovy": [2]string{"/* ", "*/"},
"hs": [2]string{"{- ", "-}"},
"kt": [2]string{commonPrefix, commonPostfix},
"rs": [2]string{commonPrefix, commonPostfix},
"html": [2]string{"<!-- ", "-->"}, //html files
"js": [2]string{commonPrefix, commonPostfix}, //javascript files
"jsx": [2]string{commonPrefix, commonPostfix}, //jsx files
"tsx": [2]string{commonPrefix, commonPostfix}, //tsx files
"css": [2]string{commonPrefix, commonPostfix}, //css files
"py": [2]string{"\"\"\"\n", "\"\"\""}, //python files
"java": [2]string{commonPrefix, commonPostfix}, //java files
"rb": [2]string{"=begin ", "=end"}, //ruby files
"c": [2]string{commonPrefix, commonPostfix}, //c files
"cpp": [2]string{commonPrefix, commonPostfix}, //c++ files
"h": [2]string{commonPrefix, commonPostfix}, //c/c++ header files
"hpp": [2]string{commonPrefix, commonPostfix}, //c++ header files
"cs": [2]string{commonPrefix, commonPostfix}, //c# files
"m": [2]string{commonPrefix, commonPostfix}, //objective c files
"go": [2]string{commonPrefix, commonPostfix}, //go files
"swift": [2]string{commonPrefix, commonPostfix}, //swift files
"clj": [2]string{"(comment ", ")"}, //clojure files
"ts": [2]string{commonPrefix, commonPostfix}, //typescript files
"dart": [2]string{commonPrefix, commonPostfix}, //dart files
"elm": [2]string{"{- ", "-}"}, //elm files
"groovy": [2]string{"/* ", "*/"}, //groovy files
"hs": [2]string{"{- ", "-}"}, //haskel files
"kt": [2]string{commonPrefix, commonPostfix}, //kotlin files
"rs": [2]string{commonPrefix, commonPostfix}, //rust files
"sh": [2]string{"#", SINGLE_LINE_COMMENTS}, //shell files
"Makefile": [2]string{"#", SINGLE_LINE_COMMENTS}, //TODO makefiles still don't work due to them not actually having a file extension
"f": [2]string{"!", SINGLE_LINE_COMMENTS}, //fortran files ext. 1
"for": [2]string{"!", SINGLE_LINE_COMMENTS}, //fortran files ext. 2
}

// Comment will give comment prefix and postfix based on programming language
Expand Down
9 changes: 7 additions & 2 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const LicensorYAML = "licensor.yml"
// UniqueIdentifier is header in comment
const UniqueIdentifier = "License generated by licensor(https://github.com/Marvin9/licensor)."

// flag for every line comments
const SINGLE_LINE_COMMENTS = ""

// flags
const (
// PROJECT: -project [projectDir]
Expand Down Expand Up @@ -54,16 +57,18 @@ var Commands = []string{
// Each may have different multiline comment syntax
var SupportedFileExtensions = []string{
"html", "js", "jsx", "tsx", "css",
"py", "java", "rb", "c", "cpp", "cs",
"py", "java", "rb", "c", "cpp", "h", "hpp", "cs",
"m", "go", "swift", "clj", "ts", "dart",
"elm", "groovy", "hs", "kt", "rs",
"elm", "groovy", "hs", "kt", "rs", "sh", "f", "for",
}

// IgnoreDirs - directories that will be ignored by default
var IgnoreDirs = []string{
"node_modules", ".git",
}

const DELIMITER = "LICENSE"

// ShouldIgnoreDir - whether dir should be ignored by default
func ShouldIgnoreDir(dir string) bool {
return Exists(dir, IgnoreDirs)
Expand Down