2016-09-01 22:17:19 +00:00
package cli
2016-07-29 23:02:11 +00:00
import (
2016-08-30 17:27:47 +00:00
"errors"
2016-07-31 21:39:56 +00:00
"fmt"
2016-08-17 23:35:37 +00:00
"github.com/fsnotify/fsnotify"
2016-08-21 13:38:00 +00:00
"log"
"math/big"
2016-07-29 23:02:11 +00:00
"os"
2016-09-05 15:53:37 +00:00
"os/signal"
2016-08-17 23:35:37 +00:00
"path/filepath"
2016-07-31 21:39:56 +00:00
"strings"
2016-08-16 10:20:55 +00:00
"sync"
2016-09-05 15:53:37 +00:00
"syscall"
2016-08-17 23:35:37 +00:00
"time"
2016-07-29 23:02:11 +00:00
)
2016-08-18 07:27:08 +00:00
// Watching method is the main core. It manages the livereload and the watching
2016-08-23 00:07:07 +00:00
func ( p * Project ) watching ( ) {
2016-08-16 10:20:55 +00:00
var wr sync . WaitGroup
2016-07-31 23:52:11 +00:00
var watcher * fsnotify . Watcher
2016-10-18 11:38:59 +00:00
sync := func ( ) {
p . parent . Sync <- "sync"
}
2016-08-17 14:56:06 +00:00
watcher , err := fsnotify . NewWatcher ( )
2016-08-17 23:35:37 +00:00
if err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( strings . ToUpper ( p . pname ( p . Name , 1 ) ) , ":" , p . Red . Bold ( err . Error ( ) ) )
2016-08-17 14:56:06 +00:00
}
2016-09-17 19:07:22 +00:00
channel , exit := make ( chan bool , 1 ) , make ( chan bool , 1 )
2016-08-17 23:35:37 +00:00
if err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , p . Red . Bold ( err . Error ( ) ) )
2016-08-17 14:56:06 +00:00
}
2016-08-17 23:35:37 +00:00
end := func ( ) {
2016-08-17 14:56:06 +00:00
watcher . Close ( )
wg . Done ( )
}
defer end ( )
2016-11-01 09:56:12 +00:00
p . path = p . Path
2016-08-17 14:56:06 +00:00
2016-09-17 19:07:22 +00:00
p . cmd ( exit )
2016-08-30 17:27:47 +00:00
err = p . walks ( watcher )
if err != nil {
2016-11-01 09:56:12 +00:00
fmt . Println ( p . pname ( p . Name , 1 ) , ":" , p . Red . Bold ( err . Error ( ) ) )
2016-08-30 17:27:47 +00:00
return
}
2016-09-05 15:53:37 +00:00
2016-08-31 12:08:15 +00:00
go p . routines ( channel , & wr )
2016-09-22 22:41:42 +00:00
p . LastChangedOn = time . Now ( ) . Truncate ( time . Second )
2016-08-24 12:03:12 +00:00
// waiting for an event
2016-08-01 17:08:37 +00:00
for {
select {
2016-08-04 22:59:41 +00:00
case event := <- watcher . Events :
2016-09-22 22:41:42 +00:00
if time . Now ( ) . Truncate ( time . Second ) . After ( p . LastChangedOn ) {
2016-08-17 23:35:37 +00:00
if event . Op & fsnotify . Chmod == fsnotify . Chmod {
2016-08-04 22:59:41 +00:00
continue
2016-08-01 17:08:37 +00:00
}
2016-08-04 22:59:41 +00:00
if _ , err := os . Stat ( event . Name ) ; err == nil {
2016-08-23 12:07:01 +00:00
var ext string
if index := strings . Index ( filepath . Ext ( event . Name ) , "_" ) ; index == - 1 {
ext = filepath . Ext ( event . Name )
} else {
ext = filepath . Ext ( event . Name )
ext = ext [ 0 : index ]
}
2016-08-04 22:59:41 +00:00
i := strings . Index ( event . Name , filepath . Ext ( event . Name ) )
2016-08-24 13:06:23 +00:00
if event . Name [ : i ] != "" && inArray ( ext , p . Watcher . Exts ) {
2016-10-17 10:18:53 +00:00
p . Buffer . StdLog = append ( p . Buffer . StdLog , BufferOut { Time : time . Now ( ) , Text : strings . ToUpper ( ext [ 1 : ] ) + " changed " + event . Name [ : i ] + ext } )
2016-10-18 11:38:59 +00:00
go sync ( )
2016-11-01 09:56:12 +00:00
fmt . Println ( p . pname ( p . Name , 4 ) , p . Magenta . Bold ( strings . ToUpper ( ext [ 1 : ] ) + " changed" ) , p . Magenta . Bold ( event . Name [ : i ] + ext ) )
2016-08-20 10:47:32 +00:00
// stop and run again
2016-08-23 08:52:00 +00:00
if p . Run {
close ( channel )
2016-08-23 14:32:25 +00:00
channel = make ( chan bool )
2016-08-23 08:52:00 +00:00
}
2016-08-24 12:03:12 +00:00
2016-08-23 20:21:15 +00:00
err := p . fmt ( event . Name [ : i ] + ext )
2016-08-24 12:03:12 +00:00
if err != nil {
2016-10-21 15:30:12 +00:00
p . Fatal ( "" , err )
2016-08-23 14:10:17 +00:00
} else {
2016-08-31 12:08:15 +00:00
go p . routines ( channel , & wr )
2016-09-22 22:41:42 +00:00
p . LastChangedOn = time . Now ( ) . Truncate ( time . Second )
2016-08-24 12:03:12 +00:00
}
2016-08-20 10:47:32 +00:00
}
2016-08-04 22:59:41 +00:00
}
}
case err := <- watcher . Errors :
2016-11-01 09:56:12 +00:00
log . Println ( p . Red . Bold ( err . Error ( ) ) )
2016-09-17 19:07:22 +00:00
case <- exit :
return
2016-08-04 19:56:42 +00:00
}
}
}
2016-08-03 16:49:37 +00:00
2016-08-24 13:32:57 +00:00
// Install calls an implementation of the "go install"
2016-08-21 13:38:00 +00:00
func ( p * Project ) install ( channel chan bool , wr * sync . WaitGroup ) {
2016-08-04 19:56:42 +00:00
if p . Bin {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , "Installing.." )
2016-08-20 14:14:11 +00:00
start := time . Now ( )
2016-08-27 20:37:46 +00:00
if std , err := p . GoInstall ( ) ; err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , fmt . Sprint ( p . Red . Bold ( err ) ) , std )
2016-08-19 20:52:53 +00:00
wr . Done ( )
2016-08-04 22:59:41 +00:00
} else {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 5 ) , ":" , p . Green . Regular ( "Installed" ) + " after" , p . Magenta . Regular ( big . NewFloat ( float64 ( time . Since ( start ) . Seconds ( ) ) ) . Text ( 'f' , 3 ) , " s" ) )
2016-08-19 20:52:53 +00:00
if p . Run {
runner := make ( chan bool , 1 )
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , "Running.." )
2016-08-20 22:29:32 +00:00
start = time . Now ( )
2016-08-19 20:52:53 +00:00
go p . GoRun ( channel , runner , wr )
for {
select {
case <- runner :
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 5 ) , ":" , p . Green . Regular ( "Has been run" ) + " after" , p . Magenta . Regular ( big . NewFloat ( float64 ( time . Since ( start ) . Seconds ( ) ) ) . Text ( 'f' , 3 ) , " s" ) )
2016-08-19 20:52:53 +00:00
return
}
}
}
2016-07-31 21:39:56 +00:00
}
2016-07-31 23:52:11 +00:00
}
2016-08-04 19:56:42 +00:00
return
2016-08-01 17:08:37 +00:00
}
2016-07-31 21:39:56 +00:00
2016-08-24 13:32:57 +00:00
// Build calls an implementation of the "go build"
2016-08-04 22:59:41 +00:00
func ( p * Project ) build ( ) {
2016-08-04 19:56:42 +00:00
if p . Build {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , "Building.." )
2016-08-20 14:14:11 +00:00
start := time . Now ( )
2016-08-27 20:37:46 +00:00
if std , err := p . GoBuild ( ) ; err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , ":" , fmt . Sprint ( p . Red . Bold ( err ) ) , std )
2016-08-04 22:59:41 +00:00
} else {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 5 ) , ":" , p . Green . Regular ( "Builded" ) + " after" , p . Magenta . Regular ( big . NewFloat ( float64 ( time . Since ( start ) . Seconds ( ) ) ) . Text ( 'f' , 3 ) , " s" ) )
2016-07-29 23:02:11 +00:00
}
2016-08-18 07:27:08 +00:00
return
2016-07-29 23:02:11 +00:00
}
2016-08-04 19:56:42 +00:00
return
}
2016-09-05 15:53:37 +00:00
// Fmt calls an implementation of the "gofmt"
2016-08-23 14:10:17 +00:00
func ( p * Project ) fmt ( path string ) error {
2016-08-21 15:12:11 +00:00
if p . Fmt {
2016-08-24 14:20:49 +00:00
if _ , err := p . GoFmt ( path ) ; err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , p . Red . Bold ( "There are some GoFmt errors in " ) , ":" , p . Magenta . Bold ( path ) )
2016-08-21 15:12:11 +00:00
}
}
return nil
}
2016-09-05 15:53:37 +00:00
// Cmd calls an wrapper for execute the commands after/before
2016-09-17 19:07:22 +00:00
func ( p * Project ) cmd ( exit chan bool ) {
2016-09-05 15:53:37 +00:00
c := make ( chan os . Signal , 2 )
signal . Notify ( c , os . Interrupt , syscall . SIGTERM )
cast := func ( commands [ ] string ) {
if errs := p . Cmd ( commands ) ; errs != nil {
for _ , err := range errs {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 2 ) , p . Red . Bold ( err ) )
2016-09-05 15:53:37 +00:00
}
}
}
if len ( p . Watcher . Before ) > 0 {
cast ( p . Watcher . Before )
}
go func ( ) {
for {
select {
case <- c :
if len ( p . Watcher . After ) > 0 {
cast ( p . Watcher . After )
}
2016-09-17 19:07:22 +00:00
close ( exit )
2016-09-05 15:53:37 +00:00
}
}
} ( )
}
// Test calls an implementation of the "go test"
2016-08-27 21:57:26 +00:00
func ( p * Project ) test ( path string ) error {
if p . Test {
if _ , err := p . GoTest ( path ) ; err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . pname ( p . Name , 1 ) , p . Red . Bold ( "Go Test fails in " ) , ":" , p . Magenta . Bold ( path ) )
2016-08-27 21:57:26 +00:00
}
}
return nil
}
2016-08-24 12:03:12 +00:00
// Walks the file tree of a project
2016-08-30 17:27:47 +00:00
func ( p * Project ) walks ( watcher * fsnotify . Watcher ) error {
2016-08-24 12:03:12 +00:00
var files , folders int64
wd , _ := os . Getwd ( )
walk := func ( path string , info os . FileInfo , err error ) error {
2016-08-24 12:17:42 +00:00
if ! p . ignore ( path ) {
if ( info . IsDir ( ) && len ( filepath . Ext ( path ) ) == 0 && ! strings . HasPrefix ( path , "." ) ) && ! strings . Contains ( path , "/." ) || ( inArray ( filepath . Ext ( path ) , p . Watcher . Exts ) ) {
if p . Watcher . Preview {
2016-11-01 09:56:12 +00:00
fmt . Println ( p . pname ( p . Name , 1 ) , ":" , path )
2016-08-24 12:17:42 +00:00
}
if err = watcher . Add ( path ) ; err != nil {
return filepath . SkipDir
}
if inArray ( filepath . Ext ( path ) , p . Watcher . Exts ) {
files ++
2016-08-24 13:30:42 +00:00
go func ( ) {
if err := p . fmt ( path ) ; err != nil {
fmt . Println ( err )
}
} ( )
2016-08-24 12:17:42 +00:00
} else {
folders ++
2016-08-27 21:57:26 +00:00
go func ( ) {
if err := p . test ( path ) ; err != nil {
fmt . Println ( err )
}
} ( )
2016-08-24 12:17:42 +00:00
}
2016-08-24 12:03:12 +00:00
}
}
return nil
}
2016-11-01 09:56:12 +00:00
if p . path == "." || p . path == "/" {
2016-08-24 12:03:12 +00:00
p . base = wd
2016-11-01 09:56:12 +00:00
p . path = p . Wdir ( )
} else if filepath . IsAbs ( p . path ) {
p . base = p . path
2016-08-24 12:03:12 +00:00
} else {
2016-11-01 09:56:12 +00:00
p . base = filepath . Join ( wd , p . path )
2016-08-24 12:03:12 +00:00
}
2016-08-30 17:27:47 +00:00
2016-08-24 12:03:12 +00:00
for _ , dir := range p . Watcher . Paths {
base := filepath . Join ( p . base , dir )
if _ , err := os . Stat ( base ) ; err == nil {
2016-08-24 12:21:46 +00:00
if err := filepath . Walk ( base , walk ) ; err != nil {
2016-11-01 09:56:12 +00:00
log . Println ( p . Red . Bold ( err . Error ( ) ) )
2016-08-24 12:03:12 +00:00
}
} else {
2016-08-30 17:27:47 +00:00
return errors . New ( base + " path doesn't exist" )
2016-08-24 12:03:12 +00:00
}
}
2016-11-01 09:56:12 +00:00
fmt . Println ( p . pname ( p . Name , 1 ) , p . Red . Bold ( "Watching" ) , p . Magenta . Bold ( files ) , "file/s" , p . Magenta . Bold ( folders ) , "folder/s" )
2016-08-30 17:27:47 +00:00
return nil
2016-08-24 12:03:12 +00:00
}
2016-08-18 07:27:08 +00:00
// Ignore validates a path
2016-08-17 14:56:06 +00:00
func ( p * Project ) ignore ( str string ) bool {
for _ , v := range p . Watcher . Ignore {
2016-08-23 00:07:07 +00:00
if strings . Contains ( str , filepath . Join ( p . base , v ) ) {
2016-08-03 08:30:45 +00:00
return true
}
}
return false
}
2016-08-23 00:23:17 +00:00
// Routines launches the following methods: run, build, fmt, install
2016-08-31 12:08:15 +00:00
func ( p * Project ) routines ( channel chan bool , wr * sync . WaitGroup ) {
2016-08-23 20:21:15 +00:00
wr . Add ( 1 )
go p . build ( )
go p . install ( channel , wr )
wr . Wait ( )
2016-08-23 00:23:17 +00:00
}
2016-11-01 09:56:12 +00:00
// Defines the colors scheme for the project name
func ( p * Project ) pname ( name string , color int ) string {
switch color {
case 1 :
name = p . Yellow . Regular ( "[" ) + strings . ToUpper ( name ) + p . Yellow . Regular ( "]" )
break
case 2 :
name = p . Yellow . Regular ( "[" ) + p . Red . Bold ( strings . ToUpper ( name ) ) + p . Yellow . Regular ( "]" )
break
case 3 :
name = p . Yellow . Regular ( "[" ) + p . Blue . Bold ( strings . ToUpper ( name ) ) + p . Yellow . Regular ( "]" )
break
case 4 :
name = p . Yellow . Regular ( "[" ) + p . Magenta . Bold ( strings . ToUpper ( name ) ) + p . Yellow . Regular ( "]" )
break
case 5 :
name = p . Yellow . Regular ( "[" ) + p . Green . Bold ( strings . ToUpper ( name ) ) + p . Yellow . Regular ( "]" )
break
}
return name
}