Esempio n. 1
0
func execSass(c *config.Config, q *registry.Queue, mode string) error {
	files, err := sassFromConfig(c, mode)
	if err != nil {
		return fmt.Errorf("read config failed")
	}
	for _, file := range files {
		args := []string{
			file.Src,
			"--cache-location", filepath.Join("temp", "sass-cache"),
		}
		if mode == "dev" {
			args = append(args, "-l")
		} else if mode == "prod" {
			args = append(args, "--style", "compressed")
		}
		output, err := utils.Exec("sass", args)
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("compiler error: %s", err)
		}

		if err := utils.WriteFile(file.Dest, output); err != nil {
			return fmt.Errorf("write file failed: %s", err)
		}

		if *config.Verbose {
			log.Printf("created file %s\n", file.Dest)
		}
	}
	return nil
}
Esempio n. 2
0
func unused(c *config.Config, q *registry.Queue) error {
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return fmt.Errorf("recursive walk error: %s", err)
		}
		if info.IsDir() {
			for _, exclude := range excludes {
				if exclude == path {
					return filepath.SkipDir
				}
			}
			return nil
		}
		if filepath.Ext(path) == ".js" {
			args := []string{path, "--ignore-params", "_,_2,_3,_4,_5"}
			output, err := utils.Exec("unused", args)
			if err != nil {
				fmt.Println(output)
			}
		}
		return nil
	}

	for _, folder := range folders {
		if err := filepath.Walk(folder, walkFn); err != nil {
			return fmt.Errorf("walk error: %s", err)
		}
	}
	return nil
}
Esempio n. 3
0
func prepareDist(c *config.Config, q *registry.Queue) error {
	dirs := c.GetListRequired("dist.prepare")
	for _, from := range dirs {
		to := "temp"
		if strings.Contains(from, "->") {
			parts := strings.Split(from, "->")
			from = strings.TrimSpace(parts[0])
			to = filepath.Join("temp", strings.TrimSpace(parts[1]))
		}

		if _, err := os.Stat(from); err != nil {
			if os.IsNotExist(err) {
				continue
			}
			return fmt.Errorf("stat failed: %s", err)
		}

		if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
			return fmt.Errorf("prepare dir failed (%s): %s", to, err)
		}

		output, err := utils.Exec("cp", []string{"-r", from, to})
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("copy error: %s", err)
		}
	}
	return nil
}
Esempio n. 4
0
func doForm(c *config.Config, q *registry.Queue, mode string) error {
	if !templates.IsRegistered(mode) {
		return fmt.Errorf("unrecognized template mode: %s", mode)
	}
	templates.SetMode(mode)

	filename := q.NextTask()
	if filename == "" {
		return fmt.Errorf("validator filename not passed as an argument")
	}
	q.RemoveNextTask()

	form, err := parseForm(filename)
	if err != nil {
		return fmt.Errorf("parse form failed: %s", err)
	}

	result := strings.Replace(form.Build(), "'", `'"'"'`, -1)
	args := []string{
		"-c", fmt.Sprintf(`echo -n '%s' | xsel -bi`, result),
	}
	output, err := utils.Exec("bash", args)
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("bash error: %s", err)
	}

	return nil
}
Esempio n. 5
0
func execRecess(c *config.Config, q *registry.Queue, mode string) error {
	files, err := lessFromConfig(c, mode)
	if err != nil {
		return fmt.Errorf("read config failed: %s", err)
	}

	var flag string
	if mode == "dev" {
		flag = "--compile"
	} else if mode == "prod" {
		flag = "--compress"
	}

	for _, file := range files {
		args := []string{flag, "--stripColors", file.Src}
		output, err := utils.Exec("recess", args)
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("tool error: %s", err)
		}

		if err := utils.WriteFile(file.Dest, output); err != nil {
			return fmt.Errorf("write file failed: %s", err)
		}

		if *config.Verbose {
			log.Printf("created file %s\n", file.Dest)
		}
	}

	return nil
}
Esempio n. 6
0
func fixlint(c *config.Config, q *registry.Queue) error {
	for _, folder := range folders {
		args := []string{"--strict", "-r", folder, "-e", "app/scripts/vendor"}
		output, err := utils.Exec("fixjsstyle", args)
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("fixer error: %s", err)
		}
	}
	return nil
}
Esempio n. 7
0
func update(c *config.Config, q *registry.Queue) error {
	// Fetch last commits, both localy & remotely
	latestSha, err := fetchLatestCommit()
	if err != nil {
		return err
	}
	currentSha, err := fetchCurrentCommit()
	if err != nil {
		return err
	}

	// Couldn't retrieve current/latest commit, ignore update
	if latestSha == "" || currentSha == "" {
		if *config.Verbose {
			log.Printf("local or remote version was not retrieved correctly\n")
		}
		return nil
	}

	if err := writeCheckUpdate(); err != nil {
		return err
	}

	// No update, return directly
	if latestSha == currentSha {
		if *config.Verbose {
			log.Printf("same version detected\n")
		}
		return nil
	}

	// Perform the update
	args := []string{"get", "-u", "github.com/ernestokarim/cb"}
	output, err := utils.Exec("go", args)
	if err != nil {
		return err
	}
	if len(output) > 0 {
		fmt.Println(output)
	}

	log.Printf("%sUpdated correctly to commit: %s%s", colors.Green, latestSha[:10], colors.Reset)

	// Rerun itself with the correct args
	if err := utils.ExecCopyOutput(os.Args[0], os.Args[1:]); err != nil {
		return fmt.Errorf("error re-executing itself with the same arguments: %s", err)
	}
	os.Exit(1)

	panic("should not be reached")
}
Esempio n. 8
0
func validatorTask(c *config.Config, q *registry.Queue) error {
	output, err := utils.Exec("rm", []string{"-rf", "../app/lib/Validators"})
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("cannot remove original validators: %s", err)
	}

	rootPath := "../app/validators"
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		// Relative path
		rel, err := filepath.Rel(rootPath, path)
		if err != nil {
			return fmt.Errorf("cannot rel validator path: %s", err)
		}

		// Read file
		f, err := yaml.ReadFile(path)
		if err != nil {
			return fmt.Errorf("read validator failed: %s", err)
		}
		data := config.NewConfig(f)

		// Extract fields
		root := data.GetDefault("root", "Object")
		if root != "Object" && root != "Array" {
			return fmt.Errorf("invalid root type, only 'object' and 'array' are accepted")
		}
		fields := parseFields(data, "fields")

		// Generate validator
		if err := generator(rel, root, fields); err != nil {
			return fmt.Errorf("generator error: %s", err)
		}

		return nil
	}
	if err := filepath.Walk(rootPath, walkFn); err != nil {
		return fmt.Errorf("walk validators failed: %s", err)
	}

	return nil
}
Esempio n. 9
0
func fetchCurrentCommit() (string, error) {
	path := utils.PackagePath("github.com/ernestokarim/cb")

	args := []string{
		"--git-dir", filepath.Join(path, ".git"),
		"--work-tree", path,
		"rev-parse",
		"HEAD",
	}
	output, err := utils.Exec("git", args)
	if err != nil {
		return "", fmt.Errorf("cannot parse git head revision: %s", err)
	}

	return strings.TrimSpace(output), nil
}
Esempio n. 10
0
func jpegtran(src, dest string) error {
	if *config.Verbose {
		log.Printf("optimizing jpeg `%s`\n", src)
	}

	args := []string{
		"-copy", "none",
		"-optimize", "-progressive",
		"-outfile", dest, src,
	}
	output, err := utils.Exec("jpegtran", args)
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("jpeg optimizer error: %s", err)
	}

	return nil
}
Esempio n. 11
0
func htmlcompressor(src, dest string) error {
	base := utils.PackagePath(selfPkg)
	jarFile := filepath.Join(base, "htmlcompressor-1.5.3.jar")

	args := []string{
		"-jar", jarFile,
		"--type", "html",
		"-o", dest,
		"-r", src,
	}
	output, err := utils.Exec("java", args)
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("compressor error: %s", err)
	}

	return nil
}
Esempio n. 12
0
func copyDist(c *config.Config, q *registry.Queue) error {
	dirs := c.GetListRequired("dist.final")

	changes := utils.LoadChanges()
	for i, dir := range dirs {
		if name, ok := changes[dir]; ok {
			dir = name
		}
		dirs[i] = dir
	}

	for _, dir := range dirs {
		from := dir
		to := dir
		if strings.Contains(dir, "->") {
			parts := strings.Split(dir, "->")
			from = strings.TrimSpace(parts[0])
			to = strings.TrimSpace(parts[1])
		}
		origin := filepath.Join("temp", from)
		dest := filepath.Join("dist", to)

		if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
			return fmt.Errorf("prepare dir failed (%s): %s", dir, err)
		}

		if *config.Verbose {
			log.Printf("copy `%s`\n", origin)
		}

		output, err := utils.Exec("cp", []string{"-r", origin, dest})
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("copy error: %s", err)
		}
	}

	return nil
}
Esempio n. 13
0
func retrieveRemoteHashes(scriptsPath, user, password, host string) (map[string]string, error) {
	args := []string{user, password, host}
	output, err := utils.Exec(filepath.Join(scriptsPath, "download-hashes.sh"), args)
	if err != nil {
		fmt.Println(output)
		return nil, fmt.Errorf("download hashes script failed: %s", err)
	}

	f, err := os.Open("temp/hashes")
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("stat hashes failed: %s", err)
	}

	hashes := map[string]string{}
	if err := gob.NewDecoder(f).Decode(&hashes); err != nil {
		return nil, fmt.Errorf("cannot decode hashes: %s", err)
	}

	return hashes, nil
}
Esempio n. 14
0
func compileJs(dest string, srcs []string) error {
	destPath := filepath.Join("temp", dest)
	dir := filepath.Dir(destPath)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return fmt.Errorf("prepare dest dir failed (%s): %s", dir, err)
	}

	args := []string{}
	for _, src := range srcs {
		args = append(args, filepath.Join("temp", src))
	}
	args = append(args, "-o", destPath, "-c", "-m")

	output, err := utils.Exec("uglifyjs", args)
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("compiler error: %s", err)
	}
	if *config.Verbose {
		log.Printf("compile file `%s` with %d sources\n", dest, len(srcs))
	}
	return nil
}
Esempio n. 15
0
func optipng(src, dest string) error {
	if *config.Verbose {
		log.Printf("optimizing png `%s`\n", src)
	}

	args := []string{
		"-strip", "all", "-clobber",
		"-out", dest, src,
	}
	output, err := utils.Exec("optipng", args)
	if err != nil {
		fmt.Println(output)
		return fmt.Errorf("png optimizer error: %s", err)
	}

	if err := os.Remove(dest + ".bak"); err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return fmt.Errorf("remove backup file failed: %s", err)
	}

	return nil
}
Esempio n. 16
0
func organizeResult(c *config.Config) error {
	excludes := c.GetListDefault("deploy.exclude")
	includes := c.GetListDefault("deploy.include")
	moves := c.GetListDefault("deploy.moves")

	// Extract list of paths to remove
	removePaths := map[string]bool{}
	walkFn := func(path string, info os.FileInfo) error {
		removePaths[path] = true
		if *config.Verbose {
			log.Printf("flag to remove `%s`...\n", path)
		}
		return nil
	}
	for _, exclude := range excludes {
		exclude = filepath.Join("..", "deploy", exclude)
		if err := utils.NewWalker(exclude).Walk(walkFn); err != nil {
			fmt.Errorf("deploy exclude walker failed: %s", err)
		}
	}

	// Cancel removing of files that are included again
	walkFn = func(path string, info os.FileInfo) error {
		cur := path
		for cur != "." {
			removePaths[cur] = false
			cur = filepath.Dir(cur)
		}
		if *config.Verbose {
			log.Printf("include `%s`...\n", path)
		}
		return nil
	}
	for _, include := range includes {
		include = filepath.Join("..", "deploy", include)
		if err := utils.NewWalker(include).Walk(walkFn); err != nil {
			fmt.Errorf("deploy include walker failed: %s", err)
		}
	}

	// Remove flagged files & folders
	for path, remove := range removePaths {
		if remove {
			if *config.Verbose {
				log.Printf("removing `%s`...\n", path)
			}
			if err := os.RemoveAll(path); err != nil {
				return fmt.Errorf("cannot remove deploy entry: %s", err)
			}
		}
	}

	// Execute move operations
	for _, move := range moves {
		parts := strings.Split(move, "->")
		origin := filepath.Join("..", "deploy", strings.TrimSpace(parts[0]))
		dest := filepath.Join("..", "deploy", strings.TrimSpace(parts[1]))

		if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
			return fmt.Errorf("cannot create dest tree structure: %s", err)
		}

		output, err := utils.Exec("cp", []string{"-r", origin, dest})
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("copy error: %s", err)
		}
	}

	return nil
}