Skip to content

Commit

Permalink
Merge entire history from desktop repo to /desktop
Browse files Browse the repository at this point in the history
Preserve history of the maputnik/desktop repository,
but move related files to `/desktop` directory,
and adjust the ci script to use it.
  • Loading branch information
nyurik committed Feb 12, 2024
2 parents b9e3289 + a304d4e commit 079c5f6
Show file tree
Hide file tree
Showing 10 changed files with 524 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/ci-desktop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: ci-desktop

on: [ push ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./desktop

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Make
run: make

- name: Test --help
run: ./bin/linux/maputnik --help

- name: Test --version
run: ./bin/linux/maputnik --version

- name: Get style
run: wget https://maputnik.github.io/osm-liberty/style.json

- name: Test --watch
run: ./bin/linux/maputnik --watch --file style.json & sleep 5; kill $!

- name: Artifacts/linux
uses: actions/upload-artifact@v3
with:
name: maputnik-linux
path: bin/linux/

- name: Artifacts/darwin
uses: actions/upload-artifact@v3
with:
name: maputnik-darwin
path: bin/darwin/

- name: Artifacts/windows
uses: actions/upload-artifact@v3
with:
name: maputnik-windows
path: bin/windows/
31 changes: 31 additions & 0 deletions desktop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
editor

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Binary version of pubilic/editor
rice-box.go

# Built binary
maputnik
21 changes: 21 additions & 0 deletions desktop/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Maputnik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions desktop/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')
BINARY=maputnik
EDITOR_VERSION ?= v1.7.0
GOPATH := $(if $(GOPATH),$(GOPATH),$(HOME)/go)
GOBIN := $(if $(GOBIN),$(GOBIN),$(HOME)/go/bin)

all: $(BINARY)

$(BINARY): $(GOBIN)/gox $(SOURCES) rice-box.go
$(GOBIN)/gox -osarch "windows/amd64 linux/amd64 darwin/amd64" -output "bin/{{.OS}}/${BINARY}"

editor/create_folder:
mkdir -p editor

editor/pull_release: editor/create_folder
# if the directory /home/runner/work/editor/editor/build/build exists, we assume that we are are running the makefile within the editor ci workflow
test -d /home/runner/work/editor/editor/build/build && echo "exists" && cd editor && cp -R /home/runner/work/editor/editor/build/build public/ || (echo "does not exist" && cd editor && rm -rf public && curl -L https://github.com/maputnik/editor/releases/download/$(EDITOR_VERSION)/public.zip --output public.zip && unzip public.zip && rm public.zip)

$(GOBIN)/gox:
go install github.com/mitchellh/gox@v1.0.1

$(GOBIN)/rice:
go install github.com/GeertJohan/go.rice/rice@v1.0.3

rice-box.go: $(GOBIN)/rice editor/pull_release
$(GOBIN)/rice embed-go

.PHONY: clean
clean:
rm -rf editor/public && rm -f rice-box.go && rm -rf bin
78 changes: 78 additions & 0 deletions desktop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Maputnik Desktop [![GitHub CI status](https://github.com/maputnik/desktop/workflows/ci/badge.svg)](https://github.com/maputnik/desktop/actions?query=workflow%3Aci)

---

A Golang based cross platform executable for integrating Maputnik locally.
This binary packages up the JavaScript and CSS bundle produced by [maputnik/editor](https://github.com/maputnik/desktop)
and embeds it in the program for easy distribution. It also allows
exposing a local style file and work on it both in Maputnik and with your favorite
editor.

Report issues on [maputnik/editor](https://github.com/maputnik/editor).

## Install

You can download a single binary for Linux, OSX or Windows from [the latest releases of **maputnik/editor**](https://github.com/maputnik/editor/releases/latest).

### Usage

Simply start up a web server and access the Maputnik editor GUI at `localhost:8000`.

```bash
maputnik
```

Expose a local style file to Maputnik allowing the web based editor
to save to the local filesystem.

```bash
maputnik --file basic-v9.json
```

Watch the local style for changes and inform the editor via web socket.
This makes it possible to edit the style with a local text editor and still
use Maputnik.

```bash
maputnik --watch --file basic-v9.json
```

Choose a local port to listen on, instead of using the default port 8000.

```bash
maputnik --port 8001
```

Specify a path to a directory which, if it exists, will be served under http://localhost:8000/static/ .
Could be used to serve sprites and glyphs.

```bash
maputnik --static ./localFolder
```

### API

`maputnik` exposes the configured styles via a HTTP API.

| Method | Description
|---------------------------------|---------------------------------------
| `GET /styles` | List the ID of all configured style files
| `GET /styles/{filename}` | Get contents of a single style file
| `PUT /styles/{filename}` | Update contents of a style file
| `WEBSOCKET /ws` | Listen to change events for the configured style files

### Build

Clone the repository. Make sure you clone it into the correct directory `$GOPATH/src/github.com/maputnik`.

```
git clone git@github.com:maputnik/desktop.git
```

Run `make` to install the 3rd party dependencies and build the `maputnik` binary embedding the editor.

```
make
```

You should now find the `maputnik` binary in your `bin` directory.
81 changes: 81 additions & 0 deletions desktop/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/gorilla/mux"
)

func StyleFileAccessor(filename string) styleFileAccessor {
return styleFileAccessor{filename, styleId(filename)}
}

func styleId(filename string) string {
raw, err := ioutil.ReadFile(filename)
if err != nil {
log.Panicln(err)
}

var spec styleSpec
err = json.Unmarshal(raw, &spec)
if err != nil {
log.Panicln(err)
}

if spec.Id == "" {
fmt.Println("No id in style")
}
return spec.Id
}

type styleSpec struct {
Id string `json:"id"`
}

// Allows access to a single style file
type styleFileAccessor struct {
filename string
id string
}

func (fa styleFileAccessor) ListFiles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
encoder.Encode([]string{fa.id})
}

func (fa styleFileAccessor) ReadFile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_ = vars["styleId"]

//TODO: Choose right file
// right now we just return the single file we know of
w.Header().Set("Content-Type", "application/json")

raw, err := ioutil.ReadFile(fa.filename)
if err != nil {
log.Panicln(err)
}
w.Write(raw)
}

func (fa styleFileAccessor) SaveFile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_ = vars["styleId"]

//TODO: Save to right file
w.Header().Set("Content-Type", "application/json")

body, _ := ioutil.ReadAll(r.Body)
var out bytes.Buffer
json.Indent(&out, body, "", " ")

if err := ioutil.WriteFile(fa.filename, out.Bytes(), 0666); err != nil {
log.Fatalf("Can not copy from request to file: %s", err.Error())
}
}
69 changes: 69 additions & 0 deletions desktop/filewatch/filewatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package filewatch

import (
"io/ioutil"
"log"
"net/http"

"github.com/fsnotify/fsnotify"
"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}

func writer(ws *websocket.Conn, filename string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Modified file:", event.Name)
var p []byte
var err error

p, err = ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}

if p != nil {
if err := ws.WriteMessage(websocket.TextMessage, p); err != nil {
return
}
}
}
case err := <-watcher.Errors:
log.Println("Watch error:", err)
}
}
}()

if err = watcher.Add(filename); err != nil {
log.Fatal(err)
}
<-done
}

func ServeWebsocketFileWatcher(filename string, w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}

writer(ws, filename)
defer ws.Close()
}
27 changes: 27 additions & 0 deletions desktop/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module maputnik/desktop

go 1.19

require (
github.com/GeertJohan/go.rice v1.0.3
github.com/fsnotify/fsnotify v1.6.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/maputnik/desktop v1.0.7
github.com/urfave/cli v1.22.12
)

require (
github.com/GeertJohan/go.incremental v1.0.0 // indirect
github.com/akavel/rsrc v0.8.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/daaku/go.zipexe v1.0.2 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/nkovacs/streamquote v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.0.1 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
)
Loading

0 comments on commit 079c5f6

Please # to comment.