Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows branch #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@ _runtime.getFileWalker("../", (fileList) => {
## OSX

### Build
Mac and Linux:
`./build-app.sh`

### Run
`./run-app.sh`

Windows(Dev):
`run-app-dev.bat`

Windows(Prod):
`run-app.bat`

### Debugging in windows
Insert this line of code into any html page that you want to debug
`<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>`
33 changes: 33 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package app

import (
"encoding/json"
"path/filepath"

"github.com/promignis/knack/constants"
"github.com/promignis/knack/fs"
"github.com/promignis/knack/utils"
)

type Manifest struct {
AppName string
}

var manifest Manifest

func parseManifest() Manifest {
if manifest == (Manifest{}) {
root := utils.GetRootPath()
manifestData := fs.GetFileData(filepath.Join(root, constants.Manifest))
json.Unmarshal(manifestData, &manifest)
}
return manifest
}

func GetAppName() string {
return parseManifest().AppName
}

func GetUserDataPath() string {
return globalSettingFolder
}
6 changes: 6 additions & 0 deletions app/config_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

import "os"

var systemSettingFolders = []string{"/Library/Application Support"}
var globalSettingFolder = os.Getenv("HOME") + "/Library/Application Support"
6 changes: 6 additions & 0 deletions app/config_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

import "os"

var systemSettingFolders = []string{os.Getenv("PROGRAMDATA")}
var globalSettingFolder = os.Getenv("APPDATA")
27 changes: 27 additions & 0 deletions app/config_xdg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !windows,!darwin

package app

import (
"os"
"path/filepath"
"strings"
)

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

var systemSettingFolders []string
var globalSettingFolder string

func init() {
if os.Getenv("XDG_CONFIG_HOME") != "" {
globalSettingFolder = os.Getenv("XDG_CONFIG_HOME")
} else {
globalSettingFolder = filepath.Join(os.Getenv("HOME"), ".config")
}
if os.Getenv("XDG_CONFIG_DIRS") != "" {
systemSettingFolders = strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":")
} else {
systemSettingFolders = []string{"/etc/xdg"}
}
}
10 changes: 10 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/promignis/knack/fs"
"github.com/promignis/knack/fuzzy"
"github.com/promignis/knack/persistance"
"github.com/promignis/knack/utils"

"github.com/zserge/webview"
Expand Down Expand Up @@ -97,6 +98,15 @@ func HandleRPC(w webview.WebView, data string) {
utils.CheckErr(err)
args := []string{string(stringified)}
HandleCallback(w, ffiData, args)
case "set_to_file":
filename := ffiData["filename"].(string)
stringifiedJson := ffiData["stringifiedJson"].(string)
persistance.Set(filename, stringifiedJson)
case "get_from_file":
filename := ffiData["filename"].(string)
stringifiedJson := persistance.Get(filename)
args := []string{stringifiedJson}
HandleCallback(w, ffiData, args)
default:
fmt.Printf("No such action %s", fnType)
}
Expand Down
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ const (
ImageFoler = "images"
DefaultIndexFile = "index.html"
RuntimeJsFile = "runtime.js"
Manifest = "manifest.json"
)
58 changes: 49 additions & 9 deletions js-runtime/runtime.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
// TODO: Add webpack to clean up pollyfilling for windows
// Pollyfilling Object.assign
if (typeof Object.assign != 'function') {
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource != null) {
for (var nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}

if(window){
(function(){
window._runtime = Object.assign(window._runtime || {}, JsRuntime());

if(window.onRuntimeLoad) {
window.onRuntimeLoad();
window.onload = function() {
if(window.onRuntimeLoad) {
window.onRuntimeLoad();
}
}

})();
Expand Down Expand Up @@ -54,7 +86,7 @@ function JsRuntime(){
sendAction({type: 'load_html', fileName: viewName})
},
loadImage: function(imageName, imageId) {
sendAction({type: 'load_img', imageName, imageId})
sendAction({type: 'load_img', imageName: imageName, imageId: imageId})
},
getFileWalker: function(filePath, cb) {
return new FileWalker(filePath, cb)
Expand All @@ -64,7 +96,13 @@ function JsRuntime(){
return new FileStat(filePath, cb)
},
fuzzyMatch: function(dict, word, distance, cb) {
sendAction({type: 'fuzzy_match', dict: JSON.stringify(dict), word, distance, callbackId: _runtime.getCbId(cb)})
sendAction({type: 'fuzzy_match', dict: JSON.stringify(dict), word: word, distance: distance, callbackId: _runtime.getCbId(cb)})
},
setToFile: function(filename, stringifiedJson) {
sendAction({type: 'set_to_file', filename: filename, stringifiedJson: stringifiedJson})
},
getFromFile: function(filename, cb) {
sendAction({type: 'get_from_file', filename: filename, callbackId: _runtime.getCbId(cb)})
}
}
}
Expand All @@ -74,9 +112,10 @@ function FileWalker(filePath, cb) {
this.filePath = filePath

this.cbId = _runtime.getCbId(cb)

this.fileStat = new FileStat(filePath, (fileStat) => {
this.fileStat = fileStat
// TODO: Add webpack instead of using _this so that arrow functions can be used in windows
_this = this
this.fileStat = new FileStat(filePath, function(fileStat, _this){
_this.fileStat = fileStat
})
this.walk()
}
Expand All @@ -92,7 +131,8 @@ function FileStat(filePath, cb) {
}

FileStat.prototype.getFileStat = function() {
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, (fileStat) => {
this.fileStat = fileStat
_this = this
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, function(fileStat){
_this.fileStat = fileStat
})
}
14 changes: 14 additions & 0 deletions js/bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var input = document.getElementById('alertText')
var btn = document.getElementById('btn')
var changeViewBtn = document.getElementById('changeView')
var persistToFileBtn = document.getElementById('persistToFile')
var getFromFileBtn = document.getElementById('getFromFile')

var currentText = ""

input.addEventListener("change", function(ev) {
Expand All @@ -15,3 +18,14 @@ changeViewBtn.addEventListener('click', function(e) {
_runtime.loadView('index2.html')
}, false)

persistToFileBtn.addEventListener('click', function() {
_runtime.setToFile("testfile", JSON.stringify({a:1}))
})

getFromFileBtn.addEventListener('click', function() {
_runtime.getFromFile("testfile", function(stringifiedJson) {
alert(stringifiedJson)
console.log(JSON.parse(stringifiedJson))
})
})

3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appName": "test-app"
}
42 changes: 42 additions & 0 deletions persistance/jsonPersistance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package persistance

import (
"os"
"path/filepath"
"sync"

"github.com/promignis/knack/app"
"github.com/promignis/knack/fs"
)

var lock sync.Mutex
var userdataFilePath = filepath.Join(app.GetUserDataPath(), "/", app.GetAppName())

func init() {

// In case the directory does not exist , create it before we start creating files inside it
if _, err := os.Stat(userdataFilePath); os.IsNotExist(err) {
err = os.MkdirAll(userdataFilePath, os.ModePerm)
if err != nil {
panic(err)
}
}
}

func Set(filename string, stringifiedJson string) {
// Putting this here so that read and write on the same file can not happen. Can be clenaed up in future.
lock.Lock()
defer lock.Unlock()
name := filename + ".json"
path := filepath.Join(userdataFilePath, name)
fs.WriteFileData(path, []byte(stringifiedJson))
}

func Get(filename string) string {
lock.Lock()
defer lock.Unlock()
name := filename + ".json"
path := filepath.Join(userdataFilePath, name)
stringifiedJson := fs.GetFileData(path)
return string(stringifiedJson)
}
2 changes: 2 additions & 0 deletions run-app-dev.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go build -o output-dev.exe
output-dev.exe
2 changes: 2 additions & 0 deletions run-app.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go build -ldflags="-H windowsgui" -o output.exe
output.exe
5 changes: 3 additions & 2 deletions views/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<!doctype html>
<html>
<script>
</script>
<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
<body>
<h1>Works!</h1>
<input id="alertText" type="text" placeholder="alert text"/>
<button id="btn">Alert me</button>
<button id="changeView">Change view</button>
<button id="persistToFile">Persist to file</button>
<button id="getFromFile">Get from file</button>
<img id="test"></img>
</body>
<script>
Expand Down