Skip to content

Commit

Permalink
Download resources from git which works in native executable as well …
Browse files Browse the repository at this point in the history
…(downloading from inside package does not work).
  • Loading branch information
buijs-dev committed May 10, 2024
1 parent 0b51666 commit 140a764
Showing 9 changed files with 116 additions and 197 deletions.
6 changes: 3 additions & 3 deletions bin/kradle.dart
Original file line number Diff line number Diff line change
@@ -64,8 +64,8 @@ Future<void> interactiveMode() async {
final prompt = Select(prompt: "choose task", options: tasks).interact();
final selection = tasks[prompt];
if (selection == addLibrary) {
final name = interact.Input(prompt: "library name").interact();
await runTaskWithSpinner(TaskName.add, {TaskOption.name: name});
final lib = interact.Input(prompt: "library name").interact();
await runTaskWithSpinner(TaskName.add, {TaskOption.lib: lib});
} else if (selection == getFlutter) {
final version = askForFlutterVersion();
final overwrite = askConfirmation("overwrite existing distribution?");
@@ -106,7 +106,7 @@ Future<void> interactiveMode() async {
final flutterVersion = askForFlutterVersion();
await runTaskWithSpinner(TaskName.init, {
TaskOption.flutter: flutterVersion,
TaskOption.klutter: gradleVersion,
TaskOption.bom: gradleVersion,
});
} else if (selection == quit) {
return;
2 changes: 1 addition & 1 deletion lib/src/cli/task_build.dart
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ class BuildProject extends Task {
Context context, Map<TaskOption, dynamic> options) async {
final workingDirectory = Directory(findPathToRoot(context, options));
// ignore: avoid_print
print("building platform module");
print("building platform module...");
_executor
..workingDirectory = workingDirectory
..arguments = ["clean", "build", "-p", "platform"]
36 changes: 33 additions & 3 deletions lib/src/cli/task_project_init.dart
Original file line number Diff line number Diff line change
@@ -34,6 +34,9 @@ import "../producer/project.dart";
import "cli.dart";
import "context.dart";

const _resourceZipUrl =
"https://github.com/buijs-dev/klutter-dart/raw/develop/resources.zip";

/// Task to prepare a flutter project for using klutter plugins.
///
/// {@category consumer}
@@ -89,7 +92,9 @@ void _consumerInit(String pathToRoot) {

Future<void> _producerInit(
String pathToRoot, String bom, VerifiedFlutterVersion flutter) async {
final resources = await _downloadResourcesZipOrThrow();
final producer = _Producer(
resourcesDirectory: resources,
pathToRoot: pathToRoot,
bomVersion: bom,
flutterVersion: flutter.version.prettyPrint);
@@ -103,12 +108,37 @@ Future<void> _producerInit(
..setupExample;
}

/// Download [_resourceZipUrl] and return the unzipped directory.
Future<Directory> _downloadResourcesZipOrThrow() async {
final zip = Directory.systemTemp.resolveFile("resources.zip")
..maybeDelete
..createSync(recursive: true);

final target = Directory.systemTemp.resolveFolder("resources_unpacked")
..maybeDelete
..createSync(recursive: true);

await download(_resourceZipUrl, zip);
if (zip.existsSync()) {
await unzip(zip, target);
zip.deleteSync();
}

if (!target.existsSync()) {
throw const KlutterException("Failed to download resources");
}

return target;
}

class _Producer {
_Producer(
{required this.bomVersion,
{required this.resourcesDirectory,
required this.bomVersion,
required this.flutterVersion,
required this.pathToRoot});

final Directory resourcesDirectory;
final String bomVersion;
final String flutterVersion;
final String pathToRoot;
@@ -201,14 +231,14 @@ extension on _Producer {
}

Future<void> get addGradle async {
final gradle = Gradle(pathToRoot);
final gradle = Gradle(pathToRoot, resourcesDirectory);
await Future.wait([
gradle.copyToRoot,
gradle.copyToAndroid,
]);
}

Future<void> get addKradle async {
await Future.wait([Kradle(pathToRoot).copyToRoot]);
await Future.wait([Kradle(pathToRoot, resourcesDirectory).copyToRoot]);
}
}
100 changes: 32 additions & 68 deletions lib/src/producer/gradle.dart
Original file line number Diff line number Diff line change
@@ -36,78 +36,46 @@ import "resource.dart";
/// {@category gradle}
class Gradle {
/// Create a Gradle instance based of the Flutter project root folder.
Gradle(this.pathToRoot);
Gradle(this.pathToRoot, this.resourcesDirectory) {
_gradlew = LocalResource(
pathToSource: resourcesDirectory.resolveFile("gradlew").absolutePath,
filename: "gradlew",
targetRelativeToRoot: "");
_gradlewBat = LocalResource(
pathToSource:
resourcesDirectory.resolveFile("gradlew.bat").absolutePath,
filename: "gradlew.bat",
targetRelativeToRoot: "");
_gradlewJar = LocalResource(
pathToSource:
resourcesDirectory.resolveFile("gradle-wrapper.jar").absolutePath,
filename: "gradle-wrapper.jar",
targetRelativeToRoot: "gradle/wrapper".normalize);
_gradlewProperties = LocalResource(
pathToSource: resourcesDirectory
.resolveFile("gradle-wrapper.properties")
.absolutePath,
filename: "gradle-wrapper.properties",
targetRelativeToRoot: "gradle/wrapper".normalize);
_gradleProperties = LocalResource(
pathToSource:
resourcesDirectory.resolveFile("gradle.properties").absolutePath,
filename: "gradle.properties",
targetRelativeToRoot: "");
}

/// The Flutter project root folder.
final String pathToRoot;

final bool _isWindows = Platform.isWindows;
/// The directory containing the gradle-wrapper files.
final Directory resourcesDirectory;

late final LocalResource _gradlew;
late final LocalResource _gradlewBat;
late final LocalResource _gradlewJar;
late final LocalResource _gradlewProperties;
late final LocalResource _gradleProperties;

late final Future<bool> _isInitialized =
Future.wait([_init]).then((_) => true);

/// Read all resources from the klutter package lib/res folder.
Future<void> get _init async {
await Future.wait([
_loadGradlew,
_loadGradlewBat,
_loadGradlewJar,
_loadGradlewProperties,
_loadGradleProperties,
]);
}

Future<void> get _loadGradlew async {
_gradlew = await loadResource(
uri: "package:klutter/res/gradlew".toUri,
targetRelativeToRoot: "",
filename: "gradlew",
isWindows: _isWindows,
);
}

Future<void> get _loadGradlewBat async {
_gradlewBat = await loadResource(
uri: "package:klutter/res/gradlew.bat".toUri,
targetRelativeToRoot: "",
filename: "gradlew.bat",
isWindows: _isWindows,
);
}

Future<void> get _loadGradlewJar async {
_gradlewJar = await loadResource(
uri: "package:klutter/res/gradle-wrapper.jar".toUri,
targetRelativeToRoot: "gradle/wrapper".normalize,
filename: "gradle-wrapper.jar",
isWindows: _isWindows,
);
}

Future<void> get _loadGradlewProperties async {
_gradlewProperties = await loadResource(
uri: "package:klutter/res/gradle-wrapper.properties".toUri,
targetRelativeToRoot: "gradle/wrapper".normalize,
filename: "gradle-wrapper.properties",
isWindows: _isWindows,
);
}

Future<void> get _loadGradleProperties async {
_gradleProperties = await loadResource(
uri: "package:klutter/res/gradle.properties".toUri,
targetRelativeToRoot: "",
filename: "gradle.properties",
isWindows: _isWindows,
);
}

/// Copy Gradle files to the project root folder.
///
/// Copies the following files:
@@ -117,13 +85,12 @@ class Gradle {
/// - gradle/wrapper/gradle-wrapper.jar
/// - gradle/wrapper/gradle-wrapper.properties
Future<void> get copyToRoot async {
await _isInitialized;
pathToRoot.verifyExists.rootFolder.copyFiles([
_gradlewBat,
_gradlew,
_gradleProperties,
_gradlewBat,
_gradlewJar,
_gradlewProperties,
_gradleProperties,
_gradlewProperties
]);
}

@@ -136,7 +103,6 @@ class Gradle {
/// - gradle/wrapper/gradle-wrapper.jar
/// - gradle/wrapper/gradle-wrapper.properties
Future<void> get copyToAndroid async {
await _isInitialized;
pathToRoot.verifyExists.androidFolder.copyFiles([
_gradlewBat,
_gradlew,
@@ -148,8 +114,6 @@ class Gradle {
}

extension on String {
Uri get toUri => Uri.parse(this);

Directory get rootFolder => Directory(this);

Directory get androidFolder =>
52 changes: 14 additions & 38 deletions lib/src/producer/kradle.dart
Original file line number Diff line number Diff line change
@@ -35,56 +35,34 @@ import "resource.dart";
/// {@category producer}
class Kradle {
/// Create a Kradle instance based of the Flutter project root folder.
Kradle(this.pathToRoot);
Kradle(this.pathToRoot, this.resourcesDirectory) {
_kradleEnv = LocalResource(
pathToSource: resourcesDirectory.resolveFile("kradle.env").absolutePath,
filename: "kradle.env",
targetRelativeToRoot: "");
_kradleYaml = LocalResource(
pathToSource:
resourcesDirectory.resolveFile("kradle.yaml").absolutePath,
filename: "kradle.yaml",
targetRelativeToRoot: "");
}

/// The Flutter project root folder.
final String pathToRoot;

final bool _isWindows = Platform.isWindows;
/// The directory containing the kradle files.
final Directory resourcesDirectory;

late final LocalResource _kradleEnv;

late final LocalResource _kradleYaml;

late final Future<bool> _isInitialized =
Future.wait([_init]).then((_) => true);

/// Read all resources from the klutter package lib/res folder.
Future<void> get _init async {
await Future.wait([
_loadKradleEnv,
_loadKradleYaml,
]);
}

Future<void> get _loadKradleEnv async {
_kradleEnv = await loadResource(
uri: "package:klutter/res/kradle.env".toUri,
targetRelativeToRoot: "".normalize,
filename: "kradle.env",
isWindows: _isWindows,
);
}

Future<void> get _loadKradleYaml async {
_kradleYaml = await loadResource(
uri: "package:klutter/res/kradle.yaml".toUri,
targetRelativeToRoot: "".normalize,
filename: "kradle.yaml",
isWindows: _isWindows,
);
}

/// Copy Kradlew files to the project root folder.
/// Copy Kradle files to the project root folder.
///
/// Copies the following files:
/// - kradle.yaml
/// - kradlew
/// - kradlew.bat
/// - kradle.env
/// - kradle/kradle-wrapper.jar
Future<void> get copyToRoot async {
await _isInitialized;
pathToRoot.verifyExists.rootFolder.copyFiles([
_kradleYaml,
_kradleEnv,
@@ -93,7 +71,5 @@ class Kradle {
}

extension on String {
Uri get toUri => Uri.parse(this);

Directory get rootFolder => Directory(this);
}
18 changes: 0 additions & 18 deletions lib/src/producer/resource.dart
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
// SOFTWARE.

import "dart:io";
import "dart:isolate";

import "../common/utilities.dart";

@@ -58,20 +57,3 @@ class LocalResource {
/// Path to the source File.
final String pathToSource;
}

/// Load resource files from lib/res folder.
Future<LocalResource> loadResource({
required Uri uri,
required String filename,
required String targetRelativeToRoot,
required bool isWindows,
}) =>
Isolate.resolvePackageUri(uri).then((pathToSource) {
return LocalResource(
pathToSource: isWindows
? pathToSource!.path.replaceFirst("/", "")
: pathToSource!.path,
filename: filename,
targetRelativeToRoot: targetRelativeToRoot,
);
});
Loading

0 comments on commit 140a764

Please sign in to comment.