Skip to content

Commit

Permalink
feat: Allow binary artifact (#16)
Browse files Browse the repository at this point in the history
* feat: Allow binary artifact

* feat: Parse Cargo.toml file to identify artifacts

* review changes 1

* add missing license header

---------

Co-authored-by: Adam Steffen <[email protected]>
  • Loading branch information
atsteffen and asteffen-partek authored Jan 27, 2023
1 parent 510a797 commit 3c321a9
Show file tree
Hide file tree
Showing 11 changed files with 545 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ hs_err_pid*

# Compiler output
**/target/

# Eclipse files
**/.project
**/.classpath
**/.settings
17 changes: 17 additions & 0 deletions rust-maven-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<artifactId>jar-jni</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -116,6 +122,17 @@
</environmentVariables>
</configuration>
</execution>
<execution>
<id>str-reverse-binary</id>
<goals>
<goal>build</goal>
</goals>
<configuration>
<path>src/main/rust/str-reverse-binary</path>
<release>true</release>
<copyTo>${project.build.directory}/bin</copyTo>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "str-reverse-binary"
version = "0.1.0"
edition = "2021"
42 changes: 42 additions & 0 deletions rust-maven-example/src/main/rust/str-reverse-binary/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2023 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

use std::env;
use std::process::ExitCode;

fn main() -> ExitCode {
let args: Vec<String> = env::args().collect();

match args.len() {
2 => {
let reversed: String = args[1].chars().rev().collect();
println!("{}", reversed);
ExitCode::SUCCESS
},
_ => {
eprintln!("Error: Must provide a single string argument.");
ExitCode::FAILURE
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2023 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

package io.questdb.example.rust;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

public class BinaryTest {

@Test
public void testBinary() throws Exception {
File binaryFile = new File("target/bin/str-reverse-binary");

Process process = new ProcessBuilder(
Arrays.asList(
binaryFile.getAbsolutePath(),
"Hello World!"))
.start();

List<String> output = new BufferedReader(new InputStreamReader(process.getInputStream()))
.lines()
.collect(Collectors.toList());

assertEquals(0, process.waitFor());
assertEquals(Arrays.asList("!dlroW olleH"), output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2023 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

package io.questdb.example.rust;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class LibTest {

@Test
public void testLibrary() {
assertEquals("Great Scott, A reversed string!: !dlroW olleH", Main.reversedString("Hello World!"));
}
}
11 changes: 11 additions & 0 deletions rust-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
<version>3.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>0.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -230,43 +228,6 @@ private File getCopyToDir() throws MojoExecutionException {
return copyToDir;
}

private File findArtifactPath() throws MojoExecutionException {
final File targetDir = getTargetDir();
final String buildType = release ? "release" : "debug";
final String osName = System.getProperty("os.name").toLowerCase();
final String libPrefix = osName.startsWith("windows") ? "" : "lib";
final String libSuffix = osName.startsWith("windows")
? ".dll" : osName.contains("mac")
? ".dylib" : ".so";
final String artifactName = libPrefix + getName().replace('-', '_') + libSuffix;
final File artifactPath = new File(targetDir, buildType + "/" + artifactName);
if (!artifactPath.exists()) {
throw new MojoExecutionException("Artifact not found: " + Shlex.quote(artifactPath.toString()));
}
return artifactPath;
}

private void copyArtifacts() throws MojoExecutionException {
if (copyTo == null) {
getLog().info("Not copying artifacts <copyTo> is not set");
return;
}

getLog().info("Copying " + getName() + "'s cdylib to " + Shlex.quote(copyTo));

final File getArtifactPath = findArtifactPath();
final File copyToDir = getCopyToDir();
final File copyToPath = new File(copyToDir, getArtifactPath.getName());
try {
Files.copy(getArtifactPath.toPath(), copyToPath.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new MojoExecutionException(
"Failed to copy " + getArtifactPath + " to " + copyToPath + ":" + e.getMessage());
}

getLog().info("Copied " + Shlex.quote(getArtifactPath.getName()));
}

@Override
public void execute() throws MojoExecutionException {
List<String> args = new ArrayList<>();
Expand Down Expand Up @@ -300,6 +261,11 @@ public void execute() throws MojoExecutionException {
Collections.addAll(args, extraArgs);
}
cargo(args);
copyArtifacts();

// if/when --out-dir is stabilized then the outputRedirector function should be replaced with just the following args:
// args.add("--out-dir")
// args.add(getCopyToDir());

new ManualOutputRedirector(getLog(), getName(), release, getPath(), getTargetDir(), getCopyToDir()).copyArtifacts();
}
}
Loading

0 comments on commit 3c321a9

Please sign in to comment.