Skip to content

Commit

Permalink
Add --sequential option to run jobs in serial
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Dec 29, 2021
1 parent 14539ee commit 54e6b05
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 78 deletions.
2 changes: 1 addition & 1 deletion cmd/minify/bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _minify_complete() {

cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
flags="-a --all --bundle --cpuprofile -l --list --match --memprofile --mime -o --output -p --preserve --preserve-links -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-no-nullish-operator --json-precision --json-keep-numbers --svg-precision -s --sync --xml-keep-whitespace"
flags="-a --all --bundle --cpuprofile -l --list --match --memprofile --mime -o --output -p --preserve --preserve-links -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-no-nullish-operator --json-precision --json-keep-numbers --svg-precision -s --sequential --sync --xml-keep-whitespace"
mimes="text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml"
types="css html js json svg xml"

Expand Down
162 changes: 85 additions & 77 deletions cmd/minify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
watch bool
sync bool
bundle bool
sequential bool
preserve []string
preserveMode bool
preserveOwnership bool
Expand Down Expand Up @@ -132,6 +133,7 @@ func run() int {
flag.Lookup("preserve").NoOptDefVal = "mode,ownership,timestamps"
flag.BoolVar(&preserveLinks, "preserve-links", false, "Copy symbolic links without dereferencing and without minifying the referenced file (only with --sync)")
flag.BoolVarP(&bundle, "bundle", "b", false, "Bundle files by concatenation into a single file")
flag.BoolVarP(&sequential, "sequential", "", false, "Minify files sequentially, not concurrently")
flag.BoolVar(&version, "version", false, "Version")

flag.StringVar(&siteurl, "url", "", "URL of file to enable URL minification")
Expand Down Expand Up @@ -392,101 +394,107 @@ func run() int {
return 1
}

numWorkers := 1
if !verbose && len(tasks) > 1 {
numWorkers = 4
if n := runtime.NumCPU(); n > numWorkers {
numWorkers = n
}
}

fails := 0
start := time.Now()

chanTasks := make(chan Task, 100)
chanFails := make(chan int, numWorkers)
for n := 0; n < numWorkers; n++ {
go minifyWorker(chanTasks, chanFails)
}

if !watch {
if !watch && (verbose || len(tasks) == 1 || sequential) {
for _, task := range tasks {
chanTasks <- task
if ok := minify(task); !ok {
fails++
}
}
} else {
watcher, err := NewWatcher(recursive)
if err != nil {
Error.Println(err)
return 1
numWorkers := runtime.NumCPU()
if verbose || len(tasks) == 1 || sequential {
numWorkers = 1
} else if numWorkers < 4 {
numWorkers = 4
}
defer watcher.Close()

changes := watcher.Run()
for _, task := range tasks {
chanTasks <- task
chanTasks := make(chan Task, 100)
chanFails := make(chan int, numWorkers)
for n := 0; n < numWorkers; n++ {
go minifyWorker(chanTasks, chanFails)
}

autoDir := false
files := roots
if !recursive {
files = inputs
}
for _, filename := range files {
watcher.AddPath(filename)
if filename == output {
autoDir = true
if !watch {
for _, task := range tasks {
chanTasks <- task
}
}
skip := map[string]bool{}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for changes != nil {
select {
case <-c:
watcher.Close()
case file, ok := <-changes:
if !ok {
changes = nil
break
} else {
watcher, err := NewWatcher(recursive)
if err != nil {
Error.Println(err)
return 1
}
defer watcher.Close()

changes := watcher.Run()
for _, task := range tasks {
chanTasks <- task
}

autoDir := false
files := roots
if !recursive {
files = inputs
}
for _, filename := range files {
watcher.AddPath(filename)
if filename == output {
autoDir = true
}
file = filepath.Clean(file)

// find longest common path among roots
root := ""
for _, path := range roots {
pathRel, err1 := filepath.Rel(path, file)
rootRel, err2 := filepath.Rel(root, file)
if err2 != nil || err1 == nil && len(pathRel) < len(rootRel) {
root = path
}
skip := map[string]bool{}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for changes != nil {
select {
case <-c:
watcher.Close()
case file, ok := <-changes:
if !ok {
changes = nil
break
}
file = filepath.Clean(file)

// find longest common path among roots
root := ""
for _, path := range roots {
pathRel, err1 := filepath.Rel(path, file)
rootRel, err2 := filepath.Rel(root, file)
if err2 != nil || err1 == nil && len(pathRel) < len(rootRel) {
root = path
}
}
}

if autoDir && root == output {
// skip files in output directory (which is also an input directory) for the first change
// skips files that are not minified and stay put as they are not explicitly copied, but that's ok
if _, ok := skip[file]; !ok {
skip[file] = true
break
if autoDir && root == output {
// skip files in output directory (which is also an input directory) for the first change
// skips files that are not minified and stay put as they are not explicitly copied, but that's ok
if _, ok := skip[file]; !ok {
skip[file] = true
break
}
}
}

if !verbose {
Info.Println(file, "changed")
}
task, err := NewTask(root, file, output, !fileMatches(file))
if err != nil {
Error.Println(err)
return 1
if !verbose {
Info.Println(file, "changed")
}
task, err := NewTask(root, file, output, !fileMatches(file))
if err != nil {
Error.Println(err)
return 1
}
chanTasks <- task
}
chanTasks <- task
}
}
}

fails := 0
close(chanTasks)
for n := 0; n < numWorkers; n++ {
fails += <-chanFails
close(chanTasks)
for n := 0; n < numWorkers; n++ {
fails += <-chanFails
}
}

if verbose && !watch {
Expand Down

0 comments on commit 54e6b05

Please sign in to comment.