Skip to content

Commit

Permalink
switched from chromedp to zserge/lorca
Browse files Browse the repository at this point in the history
  • Loading branch information
jmichiels committed Dec 1, 2019
1 parent a4b8913 commit e0be285
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 115 deletions.
2 changes: 1 addition & 1 deletion atmelstart/cmd/atstart/cmd/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// cmakeCmd represents the pull command
// cmakeCmd represents the cmake command
var cmakeCmd = &cobra.Command{
Use: "cmake",
Short: "Build the Atmel Start project using CMake",
Expand Down
2 changes: 1 addition & 1 deletion atmelstart/cmd/atstart/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// pullCmd represents the pull command
var pullCmd = &cobra.Command{
Use: "pull",
Short: "Generate code given the configuration",
Short: "Pull generated code for this project",
Run: func(cmd *cobra.Command, args []string) {
if err := atmelstart.Generate(); err != nil {
logrus.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions atmelstart/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func CMake(args []string) error {
if err != nil {
return errors.Wrap(err, "get working directory")
}
toolchain, err := findToolchainFilePath(wd)
toolchainAbs, err := findToolchainFilePath(wd)
if err != nil {
return err
}
cmd := exec.Command(`cmake`, append([]string{fmt.Sprintf(`-DCMAKE_TOOLCHAIN_FILE="%s"`, toolchain)}, args...)...)
cmd := exec.Command(`cmake`, append([]string{fmt.Sprintf(`-DCMAKE_TOOLCHAIN_FILE="%s"`, toolchainAbs)}, args...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return errors.Wrap(cmd.Run(), "run CMake")
Expand Down
107 changes: 22 additions & 85 deletions atmelstart/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package atmelstart

import (
"bytes"
"context"
"fmt"
"os"
"os/signal"
"time"

"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/zserge/lorca"
)

func Init() error {
Expand Down Expand Up @@ -48,47 +46,35 @@ const jsLoadConfig = `
`

// Returns the project configuration.
const jsSaveConfig = `ACME.ProjectSession.save()`
const jsSaveConfig = `JSON.stringify(ACME.ProjectSession.save())`

type editor struct {
ctxt context.Context
chrome lorca.UI

configYAML *configYAML

prevConfJSON configJSON
currConfJSON configJSON

// Chrome instance.
chrome *chromedp.CDP

// Channel for interrupt signal.
interrupt chan os.Signal

// Channel closed when chrome instance close.
closed chan struct{}

ticker *time.Ticker
ready bool
}

func open(configYAML *configYAML) (err error) {
e := &editor{configYAML: configYAML}

// Init context.
cancel := e.initContext()
defer cancel()

// Open atmel start in chrome.
if err := e.openChrome(); err != nil {
// Open Atmel START in Chrome.
if err != e.openChrome() {
return errors.Wrap(err, "open chrome")
}
defer e.chrome.Close()

// Setup notification on interrupt signal.
e.initNotifyInterrupt()

// Setup notification on chrome closed.
e.initNotifyChromeClose()

// Init ticker.
e.initTicker(250 * time.Millisecond)

Expand All @@ -106,17 +92,17 @@ func open(configYAML *configYAML) (err error) {
return err
}
return nil
case <-e.closed:
logrus.Info("chrome instance closed")
case <-e.chrome.Done():
logrus.Info("chrome window closed")
return nil
}
}
}

// Init cancellable context and returns cancel function.
func (e *editor) initContext() (cancel context.CancelFunc) {
e.ctxt, cancel = context.WithCancel(context.Background())
return cancel
// Opens Chrome.
func (e *editor) openChrome() (err error) {
e.chrome, err = lorca.New(e.getStartURL(), "", 1170+2*40, 800)
return err
}

// Init load ticker (running while the project is not ready).
Expand All @@ -133,23 +119,17 @@ func (e *editor) callbackTicker() (err error) {
}
}

// Returns whteter we work on a new project.
// Returns whether we work on a new project.
func (e *editor) isNewProject() bool {
return e.configYAML == nil
}

// Callback on 'load' tick.
func (e *editor) callbackLoad() (err error) {
if !e.isNewProject() {
e.ready, err = e.requestLoadConfig()
if err != nil {
return errors.Wrap(err, "loading configuration")
}
e.ready = e.requestLoadConfig()
} else {
e.ready, err = e.requestIsProjectReady()
if err != nil {
return errors.Wrap(err, "waiting configuration")
}
e.ready = e.requestIsProjectReady()
}
if e.ready {

Expand All @@ -162,20 +142,18 @@ func (e *editor) callbackLoad() (err error) {
}

// Request to load the config (if not already loading), and return if the project is ready.
func (e *editor) requestLoadConfig() (ready bool, err error) {
func (e *editor) requestLoadConfig() (ready bool) {
return e.evaluateBoolean(fmt.Sprintf(jsLoadConfig, string(e.configYAML.escape())))
}

// Request to know if the project is ready.
func (e *editor) requestIsProjectReady() (bool, error) {
func (e *editor) requestIsProjectReady() bool {
return e.evaluateBoolean(jsIsProjectReady)
}

// Callback on 'save' tick.
func (e *editor) callbackSave() error {
if err := e.requestSaveConfig(); err != nil {
errors.Wrap(err, "save project")
}
e.requestSaveConfig()
if bytes.Compare(e.prevConfJSON.Bytes(), e.currConfJSON.Bytes()) != 0 {
if e.prevConfJSON.Bytes() != nil || (e.prevConfJSON.Bytes() == nil && e.isNewProject()) {

Expand All @@ -199,10 +177,8 @@ func (e *editor) callbackSave() error {
}

// Request to save the configuration.
func (e *editor) requestSaveConfig() error {
return e.chrome.Run(e.ctxt, chromedp.Tasks{
chromedp.Evaluate(jsSaveConfig, (*[]byte)(&(e.currConfJSON.byteSlice))),
})
func (e *editor) requestSaveConfig() {
e.currConfJSON.byteSlice = []byte(e.chrome.Eval(jsSaveConfig).String())
}

// Returns start URL.
Expand All @@ -216,51 +192,12 @@ func (e *editor) getStartURL() string {
}
}

// Opens new Chrome instance.
func (e *editor) openChrome() (err error) {
e.chrome, err = chromedp.New(e.ctxt,
chromedp.WithLog(voidLogger),
chromedp.WithRunnerOptions(
runApp(e.getStartURL()),
runMaximized()))
if err != nil {
return err
}
return nil
}

// Wait from chrome to close.
func (e *editor) initNotifyChromeClose() {
e.closed = make(chan struct{})
go func() {
_ = e.chrome.Wait()
close(e.closed)
}()
}

// Wait for an interrupt signal.
func (e *editor) initNotifyInterrupt() {
e.interrupt = make(chan os.Signal, 1)
signal.Notify(e.interrupt, os.Interrupt)
}

func (e *editor) evaluateBoolean(js string) (bool, error) {
var result bool
err := e.chrome.Run(e.ctxt, chromedp.Tasks{
chromedp.Evaluate(js, &result),
})
return result, err
}

// Custom logger which does nothing.
func voidLogger(string, ...interface{}) {}

// Runner option to run chrome in app mode.
func runApp(path string) runner.CommandLineOption {
return runner.Flag("app", path)
}

// Runner option to run chrome maximized.
func runMaximized() runner.CommandLineOption {
return runner.Flag("start-maximized", "")
func (e *editor) evaluateBoolean(js string) bool {
return e.chrome.Eval(js).Bool()
}
74 changes: 49 additions & 25 deletions atmelstart/templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions example/atstart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
format_version: '2'
name: My Project
versions:
api: '1.0'
backend: 1.6.148
commit: 605f106ab95776472e3febf2fac2471441fb1816
content: 1.0.1635
content_pack_name: acme-packs-all
format: '2'
frontend: 1.6.1884
board:
identifier: CustomBoard
device: SAMD21G18A-MF
Expand Down Expand Up @@ -109,6 +117,8 @@ drivers:
nodes:
- name: CPU
input: CPU
external: false
external_frequency: 0
configuration: {}
DMAC:
user_label: DMAC
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module github.com/jmichiels/AtmelStart

require (
github.com/chromedp/chromedp v0.1.3-0.20181201115416-98d4b0de6e1b
github.com/disintegration/imaging v1.4.2 // indirect
github.com/gorilla/websocket v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pkg/errors v0.8.0
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/zserge/lorca v0.1.8
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 // indirect
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirect
)
Loading

0 comments on commit e0be285

Please sign in to comment.