How to use Haxe "package" declarations? - haxe

Why package declarations like in below two files couses compiler errors, and how to achieve having my declared package on both files (with Main included)?
File Main.hx
package foo;
class Main {
function new() {
var x:A = new A();
}
static function main() {
var main = new Main();
}
}
File A.hx
package foo;
class A {
public function new() {
trace('Hi.');
}
}

Try structuring your project like this:
[project root]
/source
/foo
Main.hx
A.hx
Then call Haxe with these arguments, with [project root] as the current working directory:
haxe -cp source --interp -main foo.Main
The name of source doesn't really matter, it could be src or Source, but the directory the .hx files are placed in needs to match their package (foo).

Related

Flashdevelop / HaxePunk: Build halted with errors

I've been trying to follow this tutorial to get started with HaxePunk. I am using FlashDevelop and have got to trying to run the program after adding the logo.png. However, when I run the program I get the following output:
Running process: C:\Program Files (x86)\FlashDevelop\Tools\fdbuild\fdbuild.exe "D:\Haxe Projects\Prj_Starting\Prj_Starting.hxproj" -ipc f201d2c5-2ffe-46d4-bb54-c67a3e34ab4a -version "3.2.1" -compiler "C:\Program Files\HaxeToolkit\haxe" -library "C:\Program Files (x86)\FlashDevelop\Library" -target "neko"
Building Prj_Starting
Running Pre-Build Command Line...
cmd: "C:\Program Files\HaxeToolkit\haxe/haxelib" run lime build "project.xml" neko -debug -Dfdb
[file_contents,C:\Program Files\HaxeToolkit\haxe\lib\lime//.current]
Build halted with errors.
Done(1)
No error specific error is given so I am unsure what is wrong. I have followed the tutorial exactly and these are my classes:
Main.hx
import com.haxepunk.Engine;
import com.haxepunk.HXP;
class Main extends Engine
{
override public function init()
{
#if debug
HXP.console.enable();
#end
HXP.scene = new MainScene();
}
public static function main() { new Main(); }
}
MainScene.hx
import com.haxepunk.Scene;
class MainScene extends Scene
{
public override function begin()
{
add(new Logo());
}
}
Logo.hx
package src;
import com.haxepunk.Entity;
import com.haxepunk.graphics.Image;
import com.haxepunk.utils.Input;
import com.haxepunk.utils.Key;
/**
* Logo entity.
* #author Abigail Smith
*/
class Logo extends Entity
{
private var speed:Int;
public function new()
{
super(270, 190);
speed = 5;
graphic = new Image("graphics/logo.png");
}
public override function update():Void {
if (Input.check(Key.RIGHT)) {
moveBy(speed, 0);
}
if (Input.check(Key.LEFT)) {
moveBy(-speed, 0);
}
if (Input.check(Key.DOWN)) {
moveBy(0, speed);
}
if (Input.check(Key.UP)) {
moveBy(0, -speed);
}
}
}
Any help with this solving this error would be appreciated. Thank you :)
It looks like you have a problem with one of the library you need that called "lime".
[file_contents,C:\Program Files\HaxeToolkit\haxe\lib\lime//.current]
Open cmd and type haxelib list
Check if you can see lime library there
If it's there then run haxelib update lime else you need to install it by running haxelib install lime
Hope this solves your problem!

How to use multiple classes in multiple files in scripts?

I need to make a standalone Groovy script that does not require compilation and runs without Groovy installed. It works well, but it fails to recognize any other script than the main script.
My folder structure is the following:
libs\
groovy-all-2.4.3.jar
ivy-2.4.0.jar
src\
makeRelease.groovy
ReleaseHelper.groovy
I am launching the script this way from the src folder:
java -cp "../libs/*" makeRelease.groovy
makeRelease looks like this:
public class makeRelease {
public static void main(String... args) {
new ReleaseHelper()
...
}
}
When run this is the output:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
src\makeRelease.groovy: 5: unable to resolve class ReleaseHelper
How can I include other classes (that reside in separate files) in such portable scripts?
I think that it is easier than you think:
libs\
groovy-all-2.4.3.jar
src\
main.groovy
Greeter.groovy
Where main.groovy
public class Main {
public static void main(args) {
println 'Main script starting...'
def greeter = new Greeter()
greeter.sayHello()
}
}
and Greeter.groovy
class Greeter {
def sayHello() {
println 'Hello!'
}
}
Simply add to the classpath the folders where you have the classes in separate files:
java -cp .;..\libs\groovy-all-2.4.3.jar groovy.ui.GroovyMain main.groovy
The above yields:
Main script starting...
Hello!

How to instantiate a FileTree in a Groovy class managed by Gradle?

I have a Gradle build script, that grew too big so I made a utility class.
In this class I want to use the Gradle fileTree (or any other Gradle class), how can I do it?
To be clear, this is in build.gradle:
ext {
utils = new Utils()
}
and in Utils.groovy (which is in buildSrc/src/main/groovy):
def chopBackgroundImage(String inPath, String outPath, int scale) {
new File(outPath).mkdirs();
def tree = fileTree(dir: inPath, include: '*.png') // doesnt work
}
fileTree is a method defined on Project interface so there's a need to pass project instance to method and import Project class in Utils. Utils should look like this:
import org.gradle.api.Project
public class Utils {
def chopBackgroundImage(Project project, String inPath, String outPath, int scale) {
new File(outPath).mkdirs();
def tree = project.fileTree(dir: inPath, include: '*.png')
}
}
To make Project accessible in buildSrc modify build.gradle by adding the following content:
buildscript {
dependencies {
gradleApi()
}
}
And - of course - because of the fact that groovy is a dynamic language chopBackgroundImage can be defined in the following way:
def chopBackgroundImage(project, inPath, outPath, scale) {
new File(outPath).mkdirs()
def tree = project.fileTree(dir: inPath, include: '*.png')
}
No dependencies needed! ;)

Groovy way to selectively mixin methods from multiple classes

I'm writing a Groovy script based on commons-io that monitors some source directory and synchronizes its files with some destination directory.
#Grab(group='commons-io', module='commons-io', version='2.4')
import org.apache.commons.io.monitor.*
import org.apache.commons.io.FileUtils
class BaseSynchronizedFileListener extends FileAlterationListenerAdaptor {
def srcDir
def destDir
/* Given a source file, returns the destination file */
File destination(File file) {
new File(destDir, file.getAbsolutePath() - srcDir.getAbsolutePath())
}
}
class CopyOnWriteListener extends BaseSynchronizedFileListener {
#Override
void onFileChange(File file) {
FileUtils.copyFile(file, destination(file))
}
#Override
void onFileCreate(File file) {
FileUtils.copyFile(file, destination(file))
}
}
class DeleteOnDeleteListener extends BaseSynchronizedFileListener {
#Override
void onFileDelete(File file) {
FileUtils.deleteQuietly(destination(file))
}
}
In addition to straight file copies, I want to support Less->CSS compilation, wherein .less files in the source directory are synchronized with .css files in the destination directory.
#Grab(group='org.lesscss', module='lesscss', version='1.3.3')
import org.lesscss.LessCompiler
class CompileLessOnWriteListener extends BaseSynchronizedFileListener {
def compiler = new LessCompiler()
#Override
File destination(File file) {
File dest = super.destination(file)
new File(dest.parentFile, dest.name - '.less' + '.css')
}
void compile(File less) {
compiler.compile(less, destination(less))
}
#Override
void onFileChange(File less) {
compile(less)
}
#Override
void onFileCreate(File less) {
compile(less)
}
}
The problem I'm encountering is when I attempt to create class DeleteCssOnDeleteLessListener to handle the situation when .less files are deleted (which, in turn, deletes the corresponding .css file) -- the code I need to do this exists in two different inheritance trees.
CompileLessOnWriteListener contains the destination() method
DeleteOnDeleteListener contains the onFileDelete() method to delete the CSS file returned by the destination() method
Is there a "Groovy way" to selectively mixin or inherit methods from both of these classes into a new class?
Or do I just need to bite the bullet and create a common super class for CompileLessOnWriteListener and DeleteCssOnDeleteLessListener?
Update
Changed the implementation. Lets see if i got the idea. You need:
Inherit two methods
"Inherit" constructor
It needs to be an instance of an interface
I think a heavy metaprogramming helps here. We can declare two objects to DeleteCssOnDeleteLessListener delegate methods to, and these objects will be accessing properties from it.
For the interface, i think you are better using the as Interface operator.
Dynamically "inherit" the constructors may get tricky. Since it is only two properties, i've declared them. You can delegate the getProperty/setProperty to one of the other two objects, if you prefer DRYing your code:
class DeleteCssOnDeleteLessListener {
def destDir, srcDir
def onLessDelete(file) {
onFileDelete destination( file )
}
}
class CompileLessOnWriteListener {
def destination(file) {
"destination $file from $srcDir"
}
}
class DeleteOnDeleteListener {
def onFileDelete(file) {
"onFileDelete $file and $destDir"
}
}
def delete = new DeleteCssOnDeleteLessListener(destDir: "dest/dir", srcDir: "src/dir")
def compileLess = new CompileLessOnWriteListener()
def deleteOnDelete = new DeleteOnDeleteListener()
delete.metaClass {
destination = compileLess.&destination
onFileDelete = deleteOnDelete.&onFileDelete
}
compileLess.metaClass.getProperty = { property -> delete.getProperty property }
deleteOnDelete.metaClass.getProperty = { property -> delete.getProperty property }
assert delete.onLessDelete("style.less") == "onFileDelete destination style.less from src/dir and dest/dir"
It's not very "Groovy", in my opinion, nor very efficient looking, but at least this approach solves my problem without having to create a common superclass:
class DeleteCssOnDeleteLessListener extends DeleteOnDeleteListener {
#Override
File destination(File f) {
new CompileLessOnWriteListener(destDir: this.destDir, srcDir: this.srcDir).destination(f)
}
}

How to call a function from one project in another one?

I have a solution consisting of two projects:
Project1
containing a single file program.cpp
namespace Program1 {
void foo() { ... }
}
Project2
containing another single file program.cpp in which I'd like to call foo()
namespace Program2 {
void bar() { Program1::foo() }
}
I have set:
project dependencies (Program2 depends on Program1)
references in Program2 to Program1
But still I recieve an error
error C2653: 'Program1' : is not a class or namespace name
What else do I need to do to call Program1::foo() in Project2?
You need to include the header file which contains the declaration:
Project1 - Program.h
namespace Program1 {
void foo();
}
Project2 - Program.cpp
#include "Program.h"
namespace Program2 {
void bar() { Program1::foo() }
}

Resources