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

support go local module #74

Merged
merged 4 commits into from
Dec 28, 2021
Merged
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
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ test: install ## commands test
pytest: ## python test
python setup.py test

tox: install-test-deps
tox: install-test-deps ## run tox
tox .

flake8: install-test-deps
flake8: install-test-deps ## run tox flake8 only
tox -e flake8 .

help: ## Display this help screen
@grep -E '^[a-zA-Z][a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sed -e 's/^GNUmakefile://' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

docker-build:
docker-build: ## build development docker image
docker build -t wandbox-api-dev .

docker-run:
docker-run: ## run development docker container
docker run -it --rm -v ${PWD}:/work -w /work wandbox-api-dev bash

docker-alpine:
docker-alpine: ## run python alpine container
docker run -it --rm -v ${PWD}:/work -w /work python:3.8-alpine sh
# apk add make

Expand Down
2 changes: 1 addition & 1 deletion samples/command/src/go/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Makefile

GOBUILD=go build
GOBUILD=GO111MODULE=off go run
# NOTE: head version is not stable
WANDBOX_CLI_OPTIONS:=--no-head

Expand Down
7 changes: 7 additions & 0 deletions samples/command/src/go/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package main

import (
"fmt"

test1 "./test"
"./test2"
"./test3"
)

func main() {
fmt.Println("Hello, Wandbox!")
test1.Test()
test2.Test()
test3.Test()
}

// Go language references:
Expand Down
7 changes: 7 additions & 0 deletions samples/command/src/go/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test1

import "fmt"

func Test() {
fmt.Println("Test1")
}
7 changes: 7 additions & 0 deletions samples/command/src/go/test2/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test2

import "fmt"

func Test() {
fmt.Println("Test2")
}
7 changes: 7 additions & 0 deletions samples/command/src/go/test3/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test3

import "./test"

func Test() {
test.Test()
}
7 changes: 7 additions & 0 deletions samples/command/src/go/test3/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

import "fmt"

func Test() {
fmt.Println("Test3")
}
59 changes: 59 additions & 0 deletions wandbox/__go__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
import os
import re

from .runner import Runner
from .cli import CLI


class GoRunner(Runner):

IMPORT_REGEX = re.compile(r'^\s*import\s+(.*?)$')
IMPORT_MULTILINE_REGEX = re.compile(r'import\s+\((.*?)\)', re.MULTILINE | re.DOTALL)

def reset(self):
self.imported = []
self.incdirs = []

def make_code(self, file, filepath, filename):
files = dict()
code = ''
for line in file:
m = self.IMPORT_REGEX.match(line)
if m:
files.update(self.on_import_single(filepath, m.group(1).strip('(\'")')))
code += line
for m in self.IMPORT_MULTILINE_REGEX.finditer(code):
files.update(self.on_import_multiline(filepath, m.group(1)))
files[filename] = code
return files

def on_import_multiline(self, filepath, imports):
files = dict()
for line in imports.splitlines():
names = line.split()
if len(names) == 1:
files.update(self.on_import_single(filepath, names[0].strip('(\'")')))
elif len(names) > 1:
files.update(self.on_import_single(filepath, names[1].strip('(\'")')))
return files

def on_import_single(self, filepath, filename):
files = dict()
if filename.startswith('.'):
path = os.path.join(os.path.dirname(filepath), filename)
if os.path.isdir(path):
path = os.path.normpath(path)
if path not in self.imported:
self.imported.append(path)
files.update(self.on_import(path, filename))
return files

def on_import(self, filepath, packagepath):
files = dict()
for f in os.listdir(filepath):
go_filepath = os.path.join(filepath, f)
if os.path.isfile(go_filepath) and f.endswith('.go'):
files.update(self.open_code(go_filepath, go_filepath))
return files


class GoCLI(CLI):

def __init__(self, compiler=None):
Expand All @@ -14,6 +70,9 @@ def go_setup(self, lang, compiler):
help='set -gcflags -m'
)

def get_runner(self, args, options):
return GoRunner(args.language, args.compiler, args.save, args.encoding, args.retry, args.retry_wait)

def setup_runner(self, args, enable_options, disable_options, runner):
self.check_bool_option(args, 'gcflags-m', enable_options, disable_options, 'go-')
runner.has_runtime_option_raw = False
Expand Down