forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: adding package command #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fedd069
feat: adding package command
erickzanardo 734f64c
adding CI to shorebird tools
erickzanardo 522da15
improving CI
erickzanardo 4548fbd
fixing ci
erickzanardo 5b1cf3f
fixing ci
erickzanardo bbe7517
fixing formating
erickzanardo 057bcf6
fixing analyze issues
erickzanardo 7f07d6f
fix ci
erickzanardo 11d5df7
adding missing tests
erickzanardo 3758b3e
Apply suggestions from code review
erickzanardo bde128d
fixing tests
erickzanardo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Shorebird Tools 💻 | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- ".github/workflows/shorebird_tools.yaml" | ||
- "packages/shorebird_tools/lib/**" | ||
- "packages/shorebird_tools/test/**" | ||
- "packages/shorebird_tools/pubspec.yaml" | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- ".github/workflows/shorebird_tools.yaml" | ||
- "packages/shorebird_tools/lib/**" | ||
- "packages/shorebird_tools/test/**" | ||
- "packages/shorebird_tools/pubspec.yaml" | ||
|
||
jobs: | ||
semantic-pull-request: | ||
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1 | ||
|
||
build: | ||
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1 | ||
with: | ||
working_directory: packages/shorebird_tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export 'package_command.dart'; | ||
export 'sample_command.dart'; |
70 changes: 70 additions & 0 deletions
70
packages/shorebird_tools/lib/src/commands/package_command.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:archive/archive_io.dart'; | ||
import 'package:args/command_runner.dart'; | ||
import 'package:mason_logger/mason_logger.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
/// {@template package_command} | ||
/// | ||
/// `shorebird_tools package` | ||
/// A [Command] that packages a generated patch into an archive | ||
/// {@endtemplate} | ||
class PackageCommand extends Command<int> { | ||
/// {@macro package_command} | ||
PackageCommand({ | ||
required Logger logger, | ||
}) : _logger = logger { | ||
argParser | ||
..addOption( | ||
'patch', | ||
abbr: 'p', | ||
mandatory: true, | ||
help: 'The path to the patch artifact which will be packaged', | ||
) | ||
..addOption( | ||
'output', | ||
abbr: 'o', | ||
mandatory: true, | ||
help: 'Where to write the packaged patch archive', | ||
); | ||
} | ||
|
||
@override | ||
String get description => 'Packages a patch artifact into an archive'; | ||
|
||
@override | ||
String get name => 'package'; | ||
|
||
final Logger _logger; | ||
|
||
@override | ||
Future<int> run() async { | ||
final patchFilePath = argResults!['patch'] as String; | ||
final outFilePath = argResults!['output'] as String; | ||
|
||
final patchFile = File(patchFilePath); | ||
if (!patchFile.existsSync()) { | ||
_logger.err('Patch file not found at $patchFilePath'); | ||
return ExitCode.software.code; | ||
} | ||
|
||
final outFile = File(outFilePath); | ||
|
||
final bytes = patchFile.readAsBytesSync(); | ||
final archiveFile = ArchiveFile( | ||
p.basename(patchFilePath), | ||
bytes.length, | ||
bytes, | ||
); | ||
|
||
ZipEncoder() | ||
..startEncode(OutputFileStream(outFile.absolute.path)) | ||
..addFile(archiveFile) | ||
..endEncode(); | ||
|
||
_logger.info('Packaged patch at $patchFilePath to $outFilePath'); | ||
|
||
return ExitCode.success.code; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/shorebird_tools/test/src/commands/package_command_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:archive/archive_io.dart'; | ||
import 'package:mason_logger/mason_logger.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:shorebird_tools/src/command_runner.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class _MockLogger extends Mock implements Logger {} | ||
|
||
void main() { | ||
group('package', () { | ||
late Logger logger; | ||
late ShorebirdToolsCommandRunner commandRunner; | ||
late Directory testDir; | ||
|
||
setUp(() { | ||
logger = _MockLogger(); | ||
commandRunner = ShorebirdToolsCommandRunner(logger: logger); | ||
testDir = Directory.systemTemp.createTempSync('shorebird_tools_test'); | ||
}); | ||
|
||
test('packages the received patch', () async { | ||
File(p.join(testDir.path, 'patch.txt')).writeAsStringSync('banana'); | ||
|
||
final exitCode = await commandRunner.run( | ||
[ | ||
'package', | ||
'-p', | ||
p.join(testDir.path, 'patch.txt'), | ||
'-o', | ||
p.join(testDir.path, 'patch.zip'), | ||
], | ||
); | ||
|
||
expect(exitCode, ExitCode.success.code); | ||
|
||
verify( | ||
() => logger.info( | ||
'Packaged patch at ${p.join(testDir.path, 'patch.txt')} ' | ||
'to ${p.join(testDir.path, 'patch.zip')}', | ||
), | ||
).called(1); | ||
|
||
final patchFile = File(p.join(testDir.path, 'patch.zip')); | ||
expect(patchFile.existsSync(), isTrue); | ||
|
||
final extractedFolder = Directory(p.join(testDir.path, 'extracted')) | ||
..createSync(); | ||
await extractFileToDisk(patchFile.path, extractedFolder.path); | ||
|
||
// Making sure it was correctly archived and can be extracted | ||
final extractedFile = File(p.join(extractedFolder.path, 'patch.txt')); | ||
expect(extractedFile.existsSync(), isTrue); | ||
expect(extractedFile.readAsStringSync(), 'banana'); | ||
}); | ||
|
||
group('when the patch file does not exists', () { | ||
test('error and logs', () async { | ||
final exitCode = await commandRunner.run( | ||
[ | ||
'package', | ||
'-p', | ||
p.join(testDir.path, 'patch.txt'), | ||
'-o', | ||
p.join(testDir.path, 'patch.zip'), | ||
], | ||
); | ||
|
||
expect(exitCode, ExitCode.software.code); | ||
|
||
verify( | ||
() => logger.err( | ||
'Patch file not found at ${p.join(testDir.path, 'patch.txt')}', | ||
), | ||
).called(1); | ||
}); | ||
}); | ||
|
||
group('when missing option for -p', () { | ||
test('shows errors and print the correct usage', () async { | ||
final exitCode = await commandRunner.run(['package', '-p']); | ||
|
||
expect(exitCode, ExitCode.usage.code); | ||
|
||
verify(() => logger.err('Missing argument for "patch".')).called(1); | ||
verify( | ||
() => logger.info(''' | ||
Usage: shorebird_tools package [arguments] | ||
-h, --help Print this usage information. | ||
-p, --patch (mandatory) The path to the patch artifact which will be packaged | ||
-o, --output (mandatory) Where to write the packaged patch archive | ||
|
||
Run "shorebird_tools help" to see global options.'''), | ||
).called(1); | ||
}); | ||
}); | ||
|
||
group('when missing option for -o', () { | ||
test('shows errors and print the correct usage', () async { | ||
final exitCode = await commandRunner.run( | ||
[ | ||
'package', | ||
'-p', | ||
'bla', | ||
'-o', | ||
], | ||
); | ||
|
||
expect(exitCode, ExitCode.usage.code); | ||
|
||
verify(() => logger.err('Missing argument for "output".')).called(1); | ||
verify( | ||
() => logger.info(''' | ||
Usage: shorebird_tools package [arguments] | ||
-h, --help Print this usage information. | ||
-p, --patch (mandatory) The path to the patch artifact which will be packaged | ||
-o, --output (mandatory) Where to write the packaged patch archive | ||
|
||
Run "shorebird_tools help" to see global options.'''), | ||
).called(1); | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.