示例#1
0
文件: example_kar.go 项目: omeid/kar
func init() {

	kar.Run(func(build *kargar.Build) error {

		return build.Add(
			kargar.Task{

				Name:  "say-hello",
				Usage: "This tasks is self-documented, it says hello for every second.",

				Action: func(ctx context.Context) error {
					ctx.Info("Hello!")
					return nil
				},
			})
	})
}
示例#2
0
func init() {

	kar.Run(func(build *kargar.Build) error {

		build.Meta.Name = "kargar-gonzo"
		build.Meta.Usage = "A simple example application."
		build.Meta.Version = "v0.0.1"

		//Get a livereload bridge.
		lr := livereload.New(livereload.Options{LiveCSS: true})

		//Set the build meta information.
		return build.Add(

			kargar.Task{
				Name:   "libs",
				Usage:  "Download frontend dependencies.",
				Action: npm.Install("./frontend/libs/", frontend_pkgs...),
			},

			kargar.Task{
				Name:  "fonts",
				Usage: "Copy fonts to public dir.",
				Action: func(ctx context.Context) error {
					return fs.Src(ctx, "./frontend/libs/material-design-icons/iconfont/MaterialIcons-Regular.*").Then(
						path.TrimPrefix("frontend/libs/material-design-icons/iconfont"),
						fs.Dest("public/assets/fonts/"),
					)
				},
			},

			kargar.Task{
				Name:  "app.js",
				Usage: "Concat frontend JavaScript javascript into app.min.js",
				Action: func(ctx context.Context) error {
					return fs.Src(ctx, "./frontend/*.js").Then(
						util.Concat(ctx, "app.js"),
						js.Minify(),
						fs.Dest("./public/assets/"),
						lr.Reload(),
					)
				},
			},

			kargar.Task{
				Name:  "libs.js",
				Usage: "Concat frontend JavaScript libraries into lib.js",
				Action: func(ctx context.Context) error {

					return fs.Src(ctx, js_files...).Then(
						util.Concat(ctx, "libs.js"),
						js.Minify(),
						fs.Dest("./public/assets/"),
						lr.Reload(),
					)
				},
			},

			kargar.Task{
				Name:  "style",
				Usage: "Build scss files into style.css",
				Action: func(ctx context.Context) error {
					return fs.Src(ctx, "./frontend/style.scss").Then(
						path.TrimPrefix("./frontend"),
						scss.Compile(),
						css.Minify(),
						fs.Dest("./public/assets/"),
						lr.Reload(),
					)
				},
			},

			kargar.Task{
				Name:  "jade",
				Usage: "Compile jade templates into html.",
				Action: func(ctx context.Context) error {

					return fs.Src(ctx, "./frontend/*.jade").Then(
						path.TrimPrefix("frontend"),
						jade.Compile(jade.Options{
							FuncMap: map[string]interface{}{
								"livereload": lr.Client(),
							},
							Data: nil,
							Delims: jade.Delims{
								Right: ">>",
								Left:  "<<",
							},
						}),
						html.Minify(html.Options{
							KeepDefaultAttrVals: true,
						}),
						fs.Dest("public"),
						lr.Reload(),
					)
				},
			},

			kargar.Task{
				Name:  "templates.js",
				Usage: "Compile partials for angular-templateCache.",
				Action: func(ctx context.Context) error {

					return fs.Src(ctx, "./frontend/templates/*.jade").Then(
						path.TrimPrefix("frontend/templates"),
						jade.Compile(jade.Options{
							FuncMap: map[string]interface{}{
								"livereload": lr.Client(),
							},
							Data: nil,
							Delims: jade.Delims{
								Right: ">>",
								Left:  "<<",
							},
						}),
						html.Minify(html.Options{
							KeepDefaultAttrVals: true,
						}),
						ngcache.Compile(ngcache.Config{
							Name:   "templates.js",
							Module: "templates",
						}),
						js.Minify(),
						fs.Dest("public/assets/"),
						lr.Reload(),
					)
				},
			},

			kargar.Task{
				Name:        "gin",
				Usage:       "Run the Gin build server and proxy.",
				Description: gin.Description,
				Action:      gin.NewGin(&gin.Config{}, "-tags=kar"),
			},

			//Frontend requires the libs.js, js, jade, and gcss tasks, this is basically "grouping" tasks.
			kargar.Task{
				Name:   "frontend",
				Usage:  "Run frontend tasks.",
				Deps:   []string{"app.js", "libs.js", "jade", "style", "fonts", "templates.js"},
				Action: kargar.Noop(),
			},

			//The name says a lonet.
			kargar.Task{
				Name:  "watch",
				Usage: "Start watching gcss, jade, and javascript files and run crossponding tasks on change.",
				Deps:  nil, //[]string{"frontend"},
				Action: watch.WatchSet(
					build.RunFor,
					map[string][]string{
						"style":       {"./frontend/*.scss"},
						"jade":        {"./frontend/*.jade"},
						"template.js": {"./frontend/templates/*.jade"},
						"app.js":      {"./frontend/*.js"},
						"libs.js":     {"./frontend/libs/*/*.js"},
					}),
			},

			//Start a livereload server and triggered everytime anything in public folder changes.
			kargar.Task{
				Name:   "livereload",
				Usage:  "Start a tiny-lr server and monitor file changes in Pubic directory.",
				Action: lr.Start(),
			},

			//When running kar with no args, well, the "default" task is run.
			kargar.Task{
				Name:  "embed",
				Usage: "Build frontend and compile them into a resource for embedding.",
				//Deps:  []string{"frontend"},
				Action: func(ctx context.Context) error {
					return fs.Src(ctx, "public/*", "public/assets/*", "public/assets/fonts/*").Then(
						path.TrimPrefix("public/"),
						resources.Build(resources.Config{
							Pkg: "main",
							Tag: "embed",
							Var: "assets",
						}),
						fs.Dest("."),
					)
				},
			},

			//When running kar with no args, well, the "default" task is run.
			kargar.Task{
				Name:   "default",
				Usage:  "Start livereload, watch, and gin tasks.",
				Deps:   []string{"livereload", "frontend", "watch", "gin"},
				Action: kargar.Noop(),
			},
		)
	})
}