Skip to content

Commit e4dc6df

Browse files
Improvements
1 parent e345066 commit e4dc6df

File tree

7 files changed

+78
-2
lines changed

7 files changed

+78
-2
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ bin/aot/
55
bin/web/
66
scripts/
77
example/test/
8-
test/resources/files/temp/
9-
test/resources/files/temp_new/
8+
test/resources/files/temp*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:primal/compiler/errors/runtime_error.dart';
2+
import 'package:primal/compiler/models/parameter.dart';
3+
import 'package:primal/compiler/platform/base/platform_cli.dart'
4+
if (dart.library.html) 'package:primal/compiler/platform/base/platform_web.dart';
5+
import 'package:primal/compiler/runtime/node.dart';
6+
7+
class DirectoryMove extends NativeFunctionNode {
8+
DirectoryMove()
9+
: super(
10+
name: 'directory.move',
11+
parameters: [
12+
Parameter.directory('a'),
13+
Parameter.directory('b'),
14+
],
15+
);
16+
17+
@override
18+
Node node(List<Node> arguments) => NodeWithArguments(
19+
name: name,
20+
parameters: parameters,
21+
arguments: arguments,
22+
);
23+
}
24+
25+
class NodeWithArguments extends NativeFunctionNodeWithArguments {
26+
const NodeWithArguments({
27+
required super.name,
28+
required super.parameters,
29+
required super.arguments,
30+
});
31+
32+
@override
33+
Node evaluate() {
34+
final Node a = arguments[0].evaluate();
35+
final Node b = arguments[1].evaluate();
36+
37+
if ((a is DirectoryNode) && (b is DirectoryNode)) {
38+
final bool moved = PlatformInterface().directory.move(a.value, b.value);
39+
40+
return BooleanNode(moved);
41+
} else {
42+
throw InvalidArgumentTypesError(
43+
function: name,
44+
expected: parameterTypes,
45+
actual: [a.type],
46+
);
47+
}
48+
}
49+
}

lib/compiler/library/standard_library.dart

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import 'package:primal/compiler/library/directory/directory_create.dart';
7171
import 'package:primal/compiler/library/directory/directory_delete.dart';
7272
import 'package:primal/compiler/library/directory/directory_exists.dart';
7373
import 'package:primal/compiler/library/directory/directory_from_path.dart';
74+
import 'package:primal/compiler/library/directory/directory_move.dart';
7475
import 'package:primal/compiler/library/environment/env_get.dart';
7576
import 'package:primal/compiler/library/error/throw.dart';
7677
import 'package:primal/compiler/library/file/file_copy.dart';
@@ -311,6 +312,7 @@ class StandardLibrary {
311312
DirectoryDelete(),
312313
DirectoryExists(),
313314
DirectoryFromPath(),
315+
DirectoryMove(),
314316

315317
// Environment
316318
EnvGet(),

lib/compiler/platform/directory/platform_directory_base.dart

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ abstract class PlatformDirectoryBase {
1010
bool delete(Directory directory);
1111

1212
bool copy(Directory source, Directory destination);
13+
14+
bool move(Directory source, Directory destination);
1315
}

lib/compiler/platform/directory/platform_directory_cli.dart

+11
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ class PlatformDirectoryCli extends PlatformDirectoryBase {
4949
return false;
5050
}
5151
}
52+
53+
@override
54+
bool move(Directory source, Directory destination) {
55+
try {
56+
source.renameSync(destination.path);
57+
58+
return true;
59+
} catch (e) {
60+
return false;
61+
}
62+
}
5263
}

lib/compiler/platform/directory/platform_directory_web.dart

+4
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ class PlatformDirectoryWeb extends PlatformDirectoryBase {
2222
@override
2323
bool copy(Directory source, Directory destination) =>
2424
throw const UnimplementedFunctionWebError('directory.copy');
25+
26+
@override
27+
bool move(Directory source, Directory destination) =>
28+
throw const UnimplementedFunctionWebError('directory.move');
2529
}

test/runtime_test.dart

+9
Original file line numberDiff line numberDiff line change
@@ -4234,6 +4234,15 @@ void main() {
42344234
directory1 = directory.fromPath("test/resources/files/temp")
42354235
directory2 = directory.fromPath("test/resources/files/temp_new")
42364236
main = directory.copy(directory1(), directory2())
4237+
''');
4238+
checkResult(runtime, true);
4239+
});
4240+
4241+
test('file.move', () {
4242+
final Runtime runtime = getRuntime('''
4243+
directory1 = directory.fromPath("test/resources/files/temp_new")
4244+
directory2 = directory.fromPath("test/resources/files/temp_extra")
4245+
main = directory.move(directory1(), directory2())
42374246
''');
42384247
checkResult(runtime, true);
42394248
});

0 commit comments

Comments
 (0)