示例#1
0
func writeControllerRouteFile(data *controllerData) error {
	lines, err := utils.ReadLines(data.AppPath)
	if err != nil {
		return fmt.Errorf("read lines failed: %s", err)
	}

	newlines := []string{}
	processed := false
	for _, line := range lines {
		if strings.Contains(line, ".otherwise") {
			if processed {
				return fmt.Errorf(".otherwise line found twice, write " +
					"the route manually in app.js")
			}
			newlines = append(newlines, []string{
				fmt.Sprintf("      .when('%s', {\n", data.Route),
				fmt.Sprintf("        templateUrl: '/%s',\n", data.ViewName),
				fmt.Sprintf("        controller: '%s'\n", data.Name),
				"      })\n",
			}...)
			processed = true
		}
		newlines = append(newlines, line)
	}
	if len(newlines) == len(lines) {
		return fmt.Errorf(".otherwise line not found in app.js, add the " +
			"route manually")
	}

	if err := utils.WriteFile(data.AppPath, strings.Join(newlines, "")); err != nil {
		return fmt.Errorf("write file failed: %s", err)
	}

	return nil
}
示例#2
0
func minignore(c *config.Config, q *registry.Queue) error {
	base := filepath.Join("temp", filepath.Base(c.GetRequired("paths.base")))
	lines, err := utils.ReadLines(base)
	if err != nil {
		return fmt.Errorf("read base html failed: %s", err)
	}
	for i, line := range lines {
		if strings.Contains(line, "<!-- min -->") {
			matchs := minRe.FindStringSubmatch(line)
			if matchs == nil {
				return fmt.Errorf("line %d of base, not a correct min format", i+1)
			}
			src := strings.Replace(matchs[1], ".js", ".min.js", -1)
			line = fmt.Sprintf("<script src=\"%s\"></script>\n", src)
		}
		if strings.Contains(line, "<!-- ignore -->") {
			line = ""
		}
		lines[i] = line
	}

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

	return nil
}
示例#3
0
func changeReferences(path string, info os.FileInfo, err error) error {
	if err != nil {
		return fmt.Errorf("walk failed: %s", err)
	}
	if info.IsDir() {
		return nil
	}

	f, err := os.Open(path)
	if err != nil {
		return fmt.Errorf("open failed: %s", err)
	}
	defer f.Close()

	content, err := ioutil.ReadAll(f)
	if err != nil {
		return fmt.Errorf("read failed: %s", err)
	}

	s := string(content)
	for old, change := range changes {
		s = strings.Replace(s, old, change, -1)
	}

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

	return nil
}
示例#4
0
文件: sass.go 项目: ernestoalejo/cb
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
}
示例#5
0
文件: recess.go 项目: ernestoalejo/cb
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
}
示例#6
0
func compilejs(c *config.Config, q *registry.Queue) error {
	base := filepath.Join("temp", filepath.Base(c.GetRequired("paths.base")))
	lines, err := utils.ReadLines(base)
	if err != nil {
		return fmt.Errorf("read base html failed: %s", err)
	}
	for i := 0; i < len(lines); i++ {
		line := lines[i]
		if strings.Contains(line, "<!-- compile") {
			match := tagRe.FindStringSubmatch(line)
			if match == nil {
				return fmt.Errorf("incorrect compile tag, line %d", i)
			}

			start := i
			lines[i] = ""

			files := []string{}
			for !strings.Contains(line, "<!-- endcompile -->") {
				match := scriptRe.FindStringSubmatch(line)
				if match != nil {
					lines[i] = ""
					files = append(files, match[1])
				}

				i++
				if i >= len(lines) {
					return fmt.Errorf("compile js block not closed, line %d", start)
				}
				line = lines[i]
			}
			if len(files) == 0 {
				return fmt.Errorf("no files found to compile %s", match[1])
			}

			if err := compileJs(match[1], files); err != nil {
				return fmt.Errorf("compile js failed: %s", err)
			}
			line = fmt.Sprintf("<script src=\"%s\"></script>\n", match[1])
		}
		lines[i] = line
	}

	if err := utils.WriteFile(base, strings.Join(lines, "")); err != nil {
		return fmt.Errorf("write file failed: %s", err)
	}
	return nil
}
示例#7
0
文件: ngmin.go 项目: ernestoalejo/cb
func walkFn(path string, info os.FileInfo, err error) error {
	if err != nil {
		return fmt.Errorf("walk failed: %s", err)
	}
	if info.IsDir() && filepath.Base(path) == "vendor" {
		return filepath.SkipDir
	}
	if info.IsDir() {
		return nil
	}
	if filepath.Ext(path) != ".js" {
		return nil
	}

	lines, err := utils.ReadLines(path)
	if err != nil {
		return fmt.Errorf("read source failed: %s", err)
	}

	newlines := []string{}
	for i, line := range lines {
		// Functions
		funcs := []string{"factory", "directive", "config", "controller", "run"}
		used := false
		for _, f := range funcs {
			if !strings.HasPrefix(line, "m."+f+"(") {
				continue
			}

			// Easy alert of a common error
			if line[len(line)-2] == ' ' {
				return fmt.Errorf("%s:%d - final space", path, i+1)
			}

			// Line continues in the next one
			if line[len(line)-2] == ',' {
				l := line
				i++
				for {
					l = fmt.Sprintf("%s %s\n", l[:len(l)-1], strings.TrimSpace(lines[i]))
					lines[i] = ""
					i++
					if i >= len(lines) {
						return fmt.Errorf("%s:%d - cannot found function start", path, i)
					}
					if strings.Contains(l, "{") {
						line = l
						break
					}
				}
			}

			// Annotate the function
			ls, err := funcAnnotations(path, i+1, line)
			if err != nil {
				return fmt.Errorf("annotation failed: %s", err)
			}
			newlines = append(newlines, ls...)

			// Closing of functions
			found := false
			for j := i; j < len(lines); j++ {
				if lines[j] == "});\n" {
					found = true
					lines[j] = "}]);\n"
					break
				}
			}
			if !found {
				return fmt.Errorf("%s:%d - close brace not found", path, i+1)
			}

			used = true
			break
		}
		if used {
			continue
		}

		newlines = append(newlines, line)
	}

	if err := utils.WriteFile(path, strings.Join(newlines, "")); err != nil {
		return fmt.Errorf("write base html failed: %s", err)
	}

	return nil
}