func (app *AppShell) startRunner() { for task := range app.taskChan { switch task.taskType { case kTaskBuildImages: app.curError = app.buildImages(task.module) case kTaskBuildStyles: app.curError = app.buildStyles(task.module) case kTaskBuildJavaScripts: app.curError = app.buildJavaScripts(task.module) case kTaskGenAssetsMapping: app.curError = app.genAssetsMapping() case kTaskBinaryTest: app.curError = app.binaryTest(task.module) case kTaskBuildBinary: app.curError = app.buildBinary() case kTaskBinaryRestart: if app.curError == nil { if err := app.kill(); err != nil { loggers.Error("App cannot be killed, maybe you should restart the gobuildweb: %v", err) } else { if err := app.start(); err != nil { loggers.Error("App cannot be started, maybe you should restart the gobuildweb: %v", err) } } } else { loggers.Warn("You have errors with current assets and binary, please fix that ...") } fmt.Println() loggers.Info("Waiting for the file changes ...") } } }
func (app *AppShell) kill() error { if app.command != nil && (app.command.ProcessState == nil || !app.command.ProcessState.Exited()) { if runtime.GOOS == "windows" { if err := app.command.Process.Kill(); err != nil { return err } } else if err := app.command.Process.Signal(os.Interrupt); err != nil { return err } rootConfig.RLock() isGraceful := rootConfig.Package.IsGraceful rootConfig.RUnlock() if !isGraceful { // Wait for our process to die before we return or hard kill after 3 sec // when this is not a graceful server select { case <-time.After(3 * time.Second): if err := app.command.Process.Kill(); err != nil { loggers.Warn("failed to kill the app: %v", err) } } } app.command = nil } return nil }
func (s _Sprite) save(spriteImg image.Image, items []_ImageItem, fullWidth, fullHeight int) error { targetFolder := fmt.Sprintf("public/images/%s", s.entry) target := path.Join(targetFolder, s.name+".png") if file, err := os.Create(target); err != nil { return fmt.Errorf("Cannot create sprite file %s, %v", target, err) } else { defer file.Close() if err := png.Encode(file, spriteImg); err != nil { return nil } target = s.addFingerPrint(targetFolder, s.name+".png") loggers.Succ("[Sprite][%s] Saved sprite image: %s", s.entry, target) // generate the stylus file stylus := "assets/stylesheets/sprites" if err := os.MkdirAll(stylus, os.ModePerm|os.ModeDir); err != nil { return fmt.Errorf("Cannot mkdir %s, %v", stylus, err) } stylus = fmt.Sprintf("assets/stylesheets/sprites/%s_%s.styl", s.entry, s.name) if stylusFile, err := os.Create(stylus); err != nil { return fmt.Errorf("Cannot create the stylus file for sprite %s, %v", stylus, err) } else { defer stylusFile.Close() spriteEntry := SpriteEntry{ Entry: s.entry, Name: s.name, Url: fmt.Sprintf("%s/images/%s/%s", s.config.UrlPrefix, s.entry, filepath.Base(target)), Sprites: make([]SpriteImage, len(items)), Width: fullWidth / s.pixelRatio, Height: fullHeight / s.pixelRatio, } lastHeight := 0 for i, image := range items { name := image.name name = name[:len(name)-len(filepath.Ext(name))] width, height := image.Bounds().Dx(), image.Bounds().Dy() if width%s.pixelRatio != 0 || height%s.pixelRatio != 0 { loggers.Warn("You have images cannot be adjusted by the pixel ratio, %s, bounds=%v, pixelRatio=%d", image.fullpath, image.Bounds(), s.pixelRatio) } spriteEntry.Sprites[i] = SpriteImage{ Name: fmt.Sprintf("%s-%s", s.name, name), X: 0, Y: -1 * lastHeight, Width: width / s.pixelRatio, Height: height / s.pixelRatio, } lastHeight += (height + s.pixelRatio) / s.pixelRatio } if err := tmSprites.Execute(stylusFile, spriteEntry); err != nil { return fmt.Errorf("Cannot generate stylus for sprites %s, %v", spriteEntry, err) } } } return nil }