这是indexloc提供的服务,不要输入任何密码
Skip to content

Add analyzer action #14

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 5 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
analyze:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Dart
uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: Install dependencies
run: |
dart pub global activate melos
melos bootstrap

- name: Analyze code
run: melos exec dart analyze
2 changes: 2 additions & 0 deletions packages/tonik/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ There are already numerous projects available to generate Dart code from OpenAPI

This package aims to overcome these shortcomings.

Special thanks goes out to [felixwoestmann](https://github.com/felixwoestmann), as this project would not have been possible without him.

## Features

coming soon
Expand Down
4 changes: 4 additions & 0 deletions packages/tonik/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ include: package:very_good_analysis/analysis_options.yaml
linter:
rules:
public_member_api_docs: false

analyzer:
errors:
avoid_print: ignore
65 changes: 35 additions & 30 deletions packages/tonik/bin/tonik.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:logging/logging.dart';
import 'package:tonik/src/openapi_loader.dart';
import 'package:tonik_core/tonik_core.dart';
import 'package:tonik_parse/tonik_parse.dart';
import 'package:tonik_generate/tonik_generate.dart';
import 'package:tonik_parse/tonik_parse.dart';

const issueUrl = 'https://github.com/t-unit/tonik/issues';

Expand Down Expand Up @@ -53,14 +54,14 @@ void printUsage(ArgParser argParser) {
final Logger logger = Logger('tonik');

void main(List<String> arguments) {
final ArgParser argParser = buildParser();
final argParser = buildParser();
String logLevel;
String packageName;
String openApiPath;
String outputDir;

try {
final ArgResults results = argParser.parse(arguments);
final results = argParser.parse(arguments);

if (results.flag('help')) {
printUsage(argParser);
Expand All @@ -75,7 +76,7 @@ void main(List<String> arguments) {
print(formatException.message);
printUsage(argParser);
exit(128);
} catch (_) {
} on Object catch (_) {
printUsage(argParser);
exit(128);
}
Expand Down Expand Up @@ -110,10 +111,11 @@ void main(List<String> arguments) {
}
});

logger.info('Starting Tonik');
logger.fine('Package name: $packageName');
logger.fine('OpenAPI document: $openApiPath');
logger.fine('Output directory: $outputDir');
logger
..info('Starting Tonik')
..fine('Package name: $packageName')
..fine('OpenAPI document: $openApiPath')
..fine('Output directory: $outputDir');

Map<String, dynamic> apiSpec;
try {
Expand All @@ -122,44 +124,47 @@ void main(List<String> arguments) {
} on OpenApiLoaderException catch (e) {
logger.severe(e.message);
exit(1);
} catch (e, s) {
logger.fine('Failed to load OpenAPI document', e, s);
logger.severe(
'Unexpected error while loading OpenAPI document. '
'Make sure to run with verbose logging and report this issue at '
'$issueUrl',
);
} on Object catch (e, s) {
logger
..fine('Failed to load OpenAPI document', e, s)
..severe(
'Unexpected error while loading OpenAPI document. '
'Make sure to run with verbose logging and report this issue at '
'$issueUrl',
);
exit(1);
}

final ApiDocument apiDocument;
try {
apiDocument = Importer().import(apiSpec);
logger.info('Successfully parsed OpenAPI document');
} catch (e, s) {
logger.fine('Failed to parse OpenAPI document', e, s);
logger.severe(
'Unexpected error while parsing OpenAPI document. '
'If you think your document is valid, please run '
'with verbose logging and report this issue at $issueUrl',
);
} on Object catch (e, s) {
logger
..fine('Failed to parse OpenAPI document', e, s)
..severe('Unexpected error while parsing OpenAPI document. '
'Unexpected error while parsing OpenAPI document. '
'If you think your document is valid, please run '
'with verbose logging and report this issue at $issueUrl',
);
exit(1);
}

try {
Generator().generate(
const Generator().generate(
apiDocument: apiDocument,
outputDirectory: outputDir,
package: packageName,
);
logger.info('Successfully generated code');
} catch (e, s) {
logger.fine('Failed to generate code', e, s);
logger.severe(
'Unexpected error while generating code. '
'If you think your document is valid, please run with '
'verbose logging and report this issue at $issueUrl',
);
} on Object catch (e, s) {
logger
..fine('Failed to generate code', e, s)
..severe(
'Unexpected error while generating code. '
'If you think your document is valid, please run with '
'verbose logging and report this issue at $issueUrl',
);
exit(1);
}
}
15 changes: 8 additions & 7 deletions packages/tonik/lib/src/openapi_loader.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:io';
import 'dart:convert';
import 'dart:io';

import 'package:logging/logging.dart';
import 'package:yaml/yaml.dart';
Expand All @@ -11,28 +11,29 @@ Logger logger = Logger('openapi_loader');
///
/// Returns a Map representation of the OpenAPI document.
Map<String, dynamic> loadOpenApiDocument(String path) {
final File file = File(path);
final file = File(path);
if (!file.existsSync()) {
throw OpenApiLoaderException('OpenAPI document not found');
}

final String content = file.readAsStringSync();
final String extension = path.toLowerCase().split('.').last;
final content = file.readAsStringSync();
final extension = path.toLowerCase().split('.').last;

try {
final apiSpec = switch (extension) {
'json' => json.decode(content) as Map<String, dynamic>,
'yaml' || 'yml' => _convertYamlToMap(loadYaml(content)),
_ =>
throw OpenApiLoaderException(
'Unsupported file extension: .$extension. Must be .json, .yaml, or .yml',
'Unsupported file extension: .$extension. '
'Must be .json, .yaml, or .yml',
),
};

logger.fine('Parsed OpenAPI document as ${extension.toUpperCase()}');

return apiSpec;
} catch (e) {
} on Object catch (e) {
logger.fine('Failed to parse OpenAPI document. $e');
throw OpenApiLoaderException('Failed to parse OpenAPI document.');
}
Expand All @@ -54,7 +55,7 @@ dynamic _convertYamlNode(dynamic yaml) {
);
}
if (yaml is YamlList) {
return yaml.map((node) => _convertYamlNode(node)).toList();
return yaml.map(_convertYamlNode).toList();
}
return yaml;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/tonik/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ environment:
dependencies:
args: ^2.5.0
logging: ^1.3.0
yaml: ^3.1.3
tonik_parse: ^0.0.4
tonik_core: ^0.0.4
tonik_generate: ^0.0.4
tonik_parse: ^0.0.4
yaml: ^3.1.3

dev_dependencies:
very_good_analysis: ^7.0.0
path: ^1.9.1
test: ^1.24.0
very_good_analysis: ^7.0.0
24 changes: 12 additions & 12 deletions packages/tonik/test/src/openapi_loader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void main() {

group('loadOpenApiDocument', () {
test('loads valid JSON file', () {
final jsonFile = File(path.join(tempDir.path, 'test.json'));
jsonFile.writeAsStringSync('''
final jsonFile = File(path.join(tempDir.path, 'test.json'))
..writeAsStringSync('''
{
"openapi": "3.0.0",
"info": {
Expand All @@ -38,8 +38,8 @@ void main() {
});

test('loads valid YAML file', () {
final yamlFile = File(path.join(tempDir.path, 'test.yaml'));
yamlFile.writeAsStringSync('''
final yamlFile = File(path.join(tempDir.path, 'test.yaml'))
..writeAsStringSync('''
openapi: 3.0.0
info:
title: Test API
Expand Down Expand Up @@ -68,8 +68,8 @@ info:
});

test('throws on unsupported file extension', () {
final txtFile = File(path.join(tempDir.path, 'test.txt'));
txtFile.writeAsStringSync('invalid');
final txtFile = File(path.join(tempDir.path, 'test.txt'))
..writeAsStringSync('invalid');

expect(
() => loadOpenApiDocument(txtFile.path),
Expand All @@ -78,8 +78,8 @@ info:
});

test('throws on invalid JSON', () {
final jsonFile = File(path.join(tempDir.path, 'invalid.json'));
jsonFile.writeAsStringSync('invalid json');
final jsonFile = File(path.join(tempDir.path, 'invalid.json'))
..writeAsStringSync('invalid json');

expect(
() => loadOpenApiDocument(jsonFile.path),
Expand All @@ -94,8 +94,8 @@ info:
});

test('throws on invalid YAML', () {
final yamlFile = File(path.join(tempDir.path, 'invalid.yaml'));
yamlFile.writeAsStringSync('''
final yamlFile = File(path.join(tempDir.path, 'invalid.yaml'))
..writeAsStringSync('''
invalid yaml:
- misaligned:
wrong indentation
Expand All @@ -109,8 +109,8 @@ invalid yaml:
});

test('handles complex YAML structures', () {
final yamlFile = File(path.join(tempDir.path, 'complex.yaml'));
yamlFile.writeAsStringSync('''
final yamlFile = File(path.join(tempDir.path, 'complex.yaml'))
..writeAsStringSync('''
openapi: 3.0.0
info:
title: Complex API
Expand Down