diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c509ac1c..9623c389 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -89,6 +89,9 @@ jobs: - name: Extern tests run: cd hx2go-extern && haxe Testbed.hxml + + - name: Test examples + run: haxe scripts/test_examples.hxml - name: Go stdlib include test run: haxe scripts/include_gen.hxml diff --git a/examples/asciigraph/Main.hx b/examples/asciigraph/Main.hx new file mode 100644 index 00000000..8f019417 --- /dev/null +++ b/examples/asciigraph/Main.hx @@ -0,0 +1,7 @@ +import go.github_com.guptarohit.Asciigraph; + +function main() { + var data:Array = [3, 4, 9, 6, 2, 4, 5, 8, 5, 10, 2, 7, 2, 5, 6]; + var graph:String = Asciigraph.plot(data); + trace("\n" + graph); +} \ No newline at end of file diff --git a/examples/asciigraph/build.hxml b/examples/asciigraph/build.hxml new file mode 100644 index 00000000..92cdc723 --- /dev/null +++ b/examples/asciigraph/build.hxml @@ -0,0 +1,6 @@ +-cp examples/asciigraph +-m Main +-L hx2go +-D go-lib=github.com/guptarohit/asciigraph +--custom-target go=output/examples/asciigraph +--cmd go -C output/examples/asciigraph/main run . \ No newline at end of file diff --git a/examples/ebiten/Main.hx b/examples/ebiten/Main.hx new file mode 100644 index 00000000..d07e4a3c --- /dev/null +++ b/examples/ebiten/Main.hx @@ -0,0 +1,43 @@ +import go.github_com.hajimehoshi.ebiten.v2.Ebitenutil; +import go.github_com.hajimehoshi.ebiten.v2.Ebiten; +import go.github_com.hajimehoshi.ebiten.v2.Image; +import go.Pointer; +import go.GoInt; +import go.Error; +import go.Tuple; + +class Game { + + public function new() { + return; + } + + @:go.Export + public function draw(screen: Pointer): Void { + Ebitenutil.debugPrint(screen, "Hello, World!"); + } + + var count = 0; + + @:go.Export + public function update(): Error { + count++; + if (count > 60 * 5) { + Sys.exit(0); + } + return null; + } + + @:go.Export + @:go.Tuple("screenWidth", "screenHeight") + public function layout(outsideWidth: GoInt, outsideHeight: GoInt): Tuple<{ screenWidth: GoInt, screenHeight: GoInt }> { + return { screenWidth: 640, screenHeight: 480 }; + } + +} + +function main() { + Ebiten.setWindowSize(640, 480); + Ebiten.setWindowTitle("Hello, World!"); + Ebiten.runGame(new Game()).sure(); +} \ No newline at end of file diff --git a/examples/ebiten/build.hxml b/examples/ebiten/build.hxml new file mode 100644 index 00000000..8e189541 --- /dev/null +++ b/examples/ebiten/build.hxml @@ -0,0 +1,6 @@ +-cp examples/ebiten +-m Main +-L hx2go +-D go-lib=github.com/hajimehoshi/ebiten/v2/... +--custom-target go=output/examples/ebiten +--cmd go -C output/examples/ebiten/main run . \ No newline at end of file diff --git a/examples/go_lua/Main.hx b/examples/go_lua/Main.hx new file mode 100644 index 00000000..a568be90 --- /dev/null +++ b/examples/go_lua/Main.hx @@ -0,0 +1,7 @@ +import go.github_com.shopify.go_lua.Lua; + +function main() { + var l = Lua.newState(); + Lua.openLibraries(l); + Lua.doString(l, 'print("hello world")').sure(); +} \ No newline at end of file diff --git a/examples/go_lua/build.hxml b/examples/go_lua/build.hxml new file mode 100644 index 00000000..0982270d --- /dev/null +++ b/examples/go_lua/build.hxml @@ -0,0 +1,6 @@ +-cp examples/go_lua +-m Main +-L hx2go +-D go-lib=github.com/Shopify/go-lua/... +--custom-target go=output/examples/go_lua +--cmd go -C output/examples/go_lua/main run . \ No newline at end of file diff --git a/examples/gorm/Main.hx b/examples/gorm/Main.hx new file mode 100644 index 00000000..193f374a --- /dev/null +++ b/examples/gorm/Main.hx @@ -0,0 +1,41 @@ +import go.Os; +import go.gorm_io.gorm.Model; +import go.gorm_io.Gorm; +import go.gorm_io.driver.Sqlite; +import sys.FileSystem; + +function main() { + if (FileSystem.exists("test.db")) { + FileSystem.deleteFile("test.db"); + } + + var db = Gorm.open(Sqlite.open("test.db")).sure(); + db.autoMigrate(new Product()); + + db.create(new Product("D42", 100)); + + var product = new Product(); + db.first(product, 1); // find product with integer primary key + db.first(product, "hx_field_code = ?", "D42"); + + // update single field + db.model(product).update("hx_field_price", 200); + // update multiple fields + db.model(product).updates(new Product("F42", 200)); + db.model(product).updates({hx_field_price: 200, hx_field_code: "F43"}); + // delete + db.delete(product, 1); + +} + +@:structInit +class Product { + @:go.Tag('gorm:"embedded"') + public var model:Model; + public var code:String; + public var price:UInt; + public function new(code="", price=0) { + this.code = code; + this.price = price; + } +} \ No newline at end of file diff --git a/examples/gorm/build.hxml b/examples/gorm/build.hxml new file mode 100644 index 00000000..cb93d840 --- /dev/null +++ b/examples/gorm/build.hxml @@ -0,0 +1,7 @@ +-cp examples/gorm +-m Main +-L hx2go +-D go-lib=gorm.io/gorm/... +-D go-lib=gorm.io/driver/sqlite/... +--custom-target go=output/examples/gorm +--cmd go -C output/examples/gorm/main run . \ No newline at end of file diff --git a/examples/gtk/Main.hx b/examples/gtk/Main.hx new file mode 100644 index 00000000..d07ef996 --- /dev/null +++ b/examples/gtk/Main.hx @@ -0,0 +1,20 @@ +import go.github_com.diamondburned.gotk4.pkg.gtk.v4.Gtk; +import go.github_com.diamondburned.gotk4.pkg.gtk.v4.Application; + +class Main { + static function main() { + var app = Gtk.newApplication("com.github.diamondburned.gotk4-examples.gtk4.simple", 0b0); + app.connectActivate(() -> { + activate(app); + }); + app.run([]); + } + + static function activate(app:Application) { + var window = Gtk.newApplicationWindow(app); + window.setTitle("gotk4 Example"); + window.setChild(Gtk.newLabel("Hello from Go!")); + window.setDefaultSize(400, 300); + window.show(); + } +} \ No newline at end of file diff --git a/examples/gtk/build.hxml b/examples/gtk/build.hxml new file mode 100644 index 00000000..e77a8d7d --- /dev/null +++ b/examples/gtk/build.hxml @@ -0,0 +1,6 @@ +-cp examples/gtk +-m Main +-L hx2go +-D go-lib=github.com/diamondburned/gotk4/pkg/... +--custom-target go=output/examples/gtk +--cmd go -C output/examples/gtk/main run . \ No newline at end of file diff --git a/examples/http_fileserver/Main.hx b/examples/http_fileserver/Main.hx new file mode 100644 index 00000000..30d02dfc --- /dev/null +++ b/examples/http_fileserver/Main.hx @@ -0,0 +1,13 @@ +import go.net.Http; +import go.net.http.Dir; + +function main() { + var port = ":8082"; + trace('http file server: http://localhost$port'); + + var dir:Dir = "."; + var err = Http.listenAndServe(port, Http.fileServer(dir)); + trace(err); + // NOTE: the server will continue to run until interrupted. + // Polite shutdown is beyond the scope of this simple example. +} diff --git a/examples/http_fileserver/build.hxml b/examples/http_fileserver/build.hxml new file mode 100644 index 00000000..8a982982 --- /dev/null +++ b/examples/http_fileserver/build.hxml @@ -0,0 +1,6 @@ +-cp examples/http_fileserver +-m Main +-L hx2go +# as this code calls the Go standard library, there is no need for a '-D go-lib=' entry here +--custom-target go=output/examples/http_fileserver +--cmd go -C output/examples/http_fileserver/main run . diff --git a/examples/miqt/Main.hx b/examples/miqt/Main.hx new file mode 100644 index 00000000..9fb5bd2e --- /dev/null +++ b/examples/miqt/Main.hx @@ -0,0 +1,19 @@ +import go.github_com.mappu.miqt.Qt; +import go.Os; + +function main() { + Qt.newQApplication(Os.args); + + var btn = Qt.newQPushButton3("Hello World!"); + btn.setFixedWidth(320); + + var counter = 0; + btn.onPressed(() -> { + counter++; + btn.setText('You have clicked the button $counter time(s)!'); + }); + + btn.show(); + + Qt.qApplication_Exec(); +} \ No newline at end of file diff --git a/examples/miqt/build.hxml b/examples/miqt/build.hxml new file mode 100644 index 00000000..a8f29c58 --- /dev/null +++ b/examples/miqt/build.hxml @@ -0,0 +1,6 @@ +-cp examples/miqt +-m Main +-L hx2go +-D go-lib=github.com/mappu/miqt/... +--custom-target go=output/examples/miqt +--cmd go -C output/examples/gorm/miqt run . \ No newline at end of file diff --git a/scripts/TestExamples.hx b/scripts/TestExamples.hx new file mode 100644 index 00000000..4d94bebf --- /dev/null +++ b/scripts/TestExamples.hx @@ -0,0 +1,16 @@ +function main() { + for (dir in sys.FileSystem.readDirectory("examples")) { + // skip + switch dir { + case "miqt", "gtk", "http_fileserver", "ebiten": + continue; + default: + } + // run + var command = 'haxe examples/$dir/build.hxml'; + Sys.println(command); + var code = Sys.command(command); + if (code != 0) + Sys.exit(code); + } +} diff --git a/scripts/test_examples.hxml b/scripts/test_examples.hxml new file mode 100644 index 00000000..466edceb --- /dev/null +++ b/scripts/test_examples.hxml @@ -0,0 +1,3 @@ +-cp scripts +-m TestExamples +--interp \ No newline at end of file