Skip to content

Commit

Permalink
🦄 refactor: Add os to copy script
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 7, 2024
1 parent 6369587 commit e7add16
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swc4j_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
cd rust
cargo build -r
cargo test -r
deno run --allow-all ../scripts/ts/copy_swc4j_lib.ts -a ${{ matrix.arch }}
deno run --allow-all ../scripts/ts/copy_swc4j_lib.ts -o ${{ matrix.os }} -a ${{ matrix.arch }}
- name: Setup JDK 8
uses: actions/setup-java@v4
Expand Down
64 changes: 36 additions & 28 deletions scripts/ts/copy_swc4j_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,54 @@
Usage:
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -a x86_64
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -d -a x86_64
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -a arm64
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -d -a arm64
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -o macos -a arm64
deno run --allow-all scripts/ts/copy_swc4j_lib.ts -d -o macos -a arm64
*/

import * as flags from "https://deno.land/std/flags/mod.ts"
import * as fs from "https://deno.land/std/fs/mod.ts"
import * as path from "https://deno.land/std/path/mod.ts"

const NAMES = ['swc4j', 'libswc4j']
const NAME = 'swc4j'
const VERSION = '0.1.0'
const OS_AND_PREFIX_MAP = {
'.dll': {
prefix: 'lib',
os: 'windows',
const OS_CONFIG_MAP = {
'windows': {
sourceName: NAME,
targetName: `lib${NAME}`,
ext: '.dll',
},
'.so': {
prefix: '',
os: 'linux',
'linux': {
sourceName: `lib${NAME}`,
targetName: `lib${NAME}`,
ext: '.so',
},
'.dylib': {
prefix: '',
os: 'macos',
'macos': {
sourceName: `lib${NAME}`,
targetName: `lib${NAME}`,
ext: '.dylib',
},
}

async function copy(debug: boolean = false, arch: string = 'x86_64'): Promise<number> {
function copy(debug: boolean = false, os: string = 'windows', arch: string = 'x86_64'): number {
if (!(os in OS_CONFIG_MAP)) {
console.error(`%c${os} is not supported.`, 'color: red')
return 1;
}
const config = OS_CONFIG_MAP[os]
const scriptDirPath = path.dirname(path.fromFileUrl(import.meta.url))
const sourceDirPath = path.join(scriptDirPath, '../../rust/target', debug ? 'debug' : 'release')
const sourceFilePath = path.join(sourceDirPath, `${config.sourceName}${config.ext}`)
if (!fs.existsSync(sourceFilePath)) {
console.error(`%c${sourceFilePath} is not found.`, 'color: red')
return 1;
}
const targetDirPath = path.join(scriptDirPath, '../../src/main/resources')
if (!fs.existsSync(targetDirPath)) {
Deno.mkdirSync(targetDirPath, { recursive: true });
}
for await (const { isFile, name } of Deno.readDir(sourceDirPath)) {
if (isFile) {
const parsedName = path.parse(name)
if (NAMES.includes(parsedName.name) && parsedName.ext in OS_AND_PREFIX_MAP) {
const osAndPrefix = OS_AND_PREFIX_MAP[parsedName.ext]
const sourceFilePath = path.join(sourceDirPath, name)
const targetFilePath = path.join(targetDirPath, `${osAndPrefix.prefix}${parsedName.name}-${osAndPrefix.os}-${arch}.v.${VERSION}${parsedName.ext}`)
console.info(`Copy from ${sourceFilePath} to ${targetFilePath}.`)
fs.copySync(sourceFilePath, targetFilePath, { overwrite: true })
}
}
}
const targetFilePath = path.join(targetDirPath, `${config.targetName}-${os}-${arch}.v.${VERSION}${config.ext}`)
console.info(`Copy from ${sourceFilePath} to ${targetFilePath}.`)
fs.copySync(sourceFilePath, targetFilePath, { overwrite: true })
return 0
}

Expand All @@ -71,6 +75,7 @@ const args = flags.parse(Deno.args, {
"arch": "a",
"debug": "d",
"help": "h",
"os": "o",
"version": "v",
},
boolean: [
Expand All @@ -80,24 +85,27 @@ const args = flags.parse(Deno.args, {
],
string: [
"arch",
"os",
],
default: {
arch: "x86_64",
debug: false,
help: false,
os: "windows",
version: false,
},
})

if (args.help) {
console.info(`Usage: copy_swc4j_lib.ts
-a, --arch CPU arch (default: x86_64)
-a, --arch CPU arch [x86, x86_64, arm, arm64] (default: x86_64)
-d, --debug Copy the debug lib (default: false)
-h, --help Print this help page
-o, --os Operating system [windows, linux, macos] (default: windows)
-v, --version Print version
`)
} else if (args.version) {
console.info(`Version: ${VERSION}`)
} else {
Deno.exit(await copy(args.debug, args.arch))
Deno.exit(copy(args.debug, args.os, args.arch))
}
3 changes: 2 additions & 1 deletion src/main/java/com/caoccao/javet/swc4j/SwcLibLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.caoccao.javet.swc4j;

import com.caoccao.javet.swc4j.utils.ArrayUtils;
import com.caoccao.javet.swc4j.utils.OSUtils;
import com.caoccao.javet.swc4j.utils.StringUtils;

Expand Down Expand Up @@ -217,7 +218,7 @@ private void purge(File rootLibPath) {
if (rootLibPath.exists()) {
if (rootLibPath.isDirectory()) {
File[] files = rootLibPath.listFiles();
if (files != null && files.length > 0) {
if (ArrayUtils.isNotEmpty(files)) {
for (File libFileOrPath : files) {
if (libFileOrPath.lastModified() + MIN_LAST_MODIFIED_GAP_IN_MILLIS > System.currentTimeMillis()) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/SwcNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ final class SwcNative {
new SwcLibLoader().load();
}

private SwcNative() {
}

public static native String getVersion();
}
95 changes: 95 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/utils/ArrayUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* 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 com.caoccao.javet.swc4j.utils;

/**
* The type Array utils.
*
* @since 0.1.0
*/
public final class ArrayUtils {
private ArrayUtils() {
}

/**
* Test if the input byte array is empty.
*
* @param array the array
* @return true : empty, false : not empty
* @since 0.1.0
*/
public static boolean isEmpty(byte[] array) {
return array == null || array.length == 0;
}

/**
* Test if the input long array is empty.
*
* @param array the array
* @return true : empty, false : not empty
* @since 0.1.0
*/
public static boolean isEmpty(long[] array) {
return array == null || array.length == 0;
}

/**
* Test if the input array is empty.
*
* @param <T> the type parameter
* @param array the array
* @return true : empty, false : not empty
* @since 0.1.0
*/
public static <T> boolean isEmpty(T[] array) {
return array == null || array.length == 0;
}

/**
* Test if the input byte array is not empty.
*
* @param array the array
* @return true : not empty, false : empty
* @since 0.1.0
*/
public static boolean isNotEmpty(byte[] array) {
return array != null && array.length > 0;
}

/**
* Test if the input long array is not empty.
*
* @param array the array
* @return true : not empty, false : empty
* @since 0.1.0
*/
public static boolean isNotEmpty(long[] array) {
return array != null && array.length > 0;
}

/**
* Test if the input array is not empty.
*
* @param <T> the type parameter
* @param array the array
* @return true : not empty, false : empty
* @since 0.1.0
*/
public static <T> boolean isNotEmpty(T[] array) {
return array != null && array.length > 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/caoccao/javet/swc4j/utils/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.lang.management.ManagementFactory;

public class OSUtils {
public final class OSUtils {
public static final String OS_ARCH = System.getProperty("os.arch");
public static final String OS_NAME = System.getProperty("os.name");
public static final String JAVA_VM_NAME = System.getProperty("java.vm.name");
Expand Down

0 comments on commit e7add16

Please sign in to comment.