1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-30 16:23:23 +00:00

[linting_tool] Implement exporting profiles (#869)

This commit is contained in:
Abdullah Deshmukh
2021-08-10 09:05:59 +05:30
committed by GitHub
parent a46327ef80
commit 9986fe2f2c
16 changed files with 224 additions and 10 deletions

View File

@@ -2,16 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:json2yaml/json2yaml.dart';
import 'package:linting_tool/model/profile.dart';
import 'package:linting_tool/model/rule.dart';
import 'package:linting_tool/repository/hive_service.dart';
import 'package:linting_tool/repository/repository.dart';
import 'package:http/http.dart' as http;
import 'package:file_selector/file_selector.dart' as file_selector;
import 'package:yaml/yaml.dart';
const _boxName = 'rules_profile';
class ProfilesStore extends ChangeNotifier {
ProfilesStore() {
late final Repository repository;
ProfilesStore(http.Client httpClient) {
repository = Repository(httpClient);
fetchSavedProfiles();
}
bool _isLoading = true;
@@ -66,4 +76,84 @@ class ProfilesStore extends ChangeNotifier {
await fetchSavedProfiles();
});
}
Future<bool> exportProfileFile(
RulesProfile profile, {
RulesStyle rulesStyle = RulesStyle.booleanMap,
}) async {
_isLoading = true;
notifyListeners();
var resultSaved = false;
try {
var templateFileData = await repository.getTemplateFile();
String newYamlFile =
_prepareYamlFile(profile, templateFileData, rulesStyle);
resultSaved = await _saveFileToDisk(newYamlFile);
} on SocketException catch (e) {
log(e.toString());
_error = 'Check internet connection.';
resultSaved = false;
} on Exception catch (e) {
log(e.toString());
}
_isLoading = false;
notifyListeners();
return resultSaved;
}
Future<bool> _saveFileToDisk(String newYamlFile) async {
const name = 'analysis_options.yaml';
/// Get file path using file picker.
var savePath = await file_selector.getSavePath(
suggestedName: name,
);
final data = Uint8List.fromList(newYamlFile.codeUnits);
final file = file_selector.XFile.fromData(data, name: name);
/// Save file to disk if path was provided.
if (savePath != null) {
file.saveTo(savePath);
return true;
}
var errorMessage = 'File path not found.';
_error = errorMessage;
throw Exception(errorMessage);
}
String _prepareYamlFile(
RulesProfile profile, YamlMap templateFile, RulesStyle rulesStyle) {
var rules = profile.rules.map((e) => e.name).toList();
var rulesData =
json.decode(json.encode(templateFile)) as Map<String, dynamic>;
/// Add rules to existing template according to formatting style.
if (rulesStyle == RulesStyle.booleanMap) {
var rulesMap = Map.fromEntries(
rules.map(
(e) => MapEntry(e, true),
),
);
rulesData.update('linter', (dynamic value) => {'rules': rulesMap});
} else {
rulesData.update('linter', (dynamic value) => {'rules': rules});
}
return json2yaml(rulesData, yamlStyle: YamlStyle.pubspecYaml);
}
}
/// Formatting style for rules.
enum RulesStyle {
list,
booleanMap,
}