Skip to content
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
9 changes: 6 additions & 3 deletions monarch/lib/src/builders/builder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import 'package:build/build.dart';
// import 'package:build_runner_core/build_runner_core.dart';
import 'package:path/path.dart' as p;

const generatedOutputDirectory = '.dart_tool/build/generated';
String generatedOutputDirectory = p.join('.dart_tool','build','generated');

String getOuputPathInGeneratedOutputDirectory(AssetId inputId) {
return '$generatedOutputDirectory/${inputId.package}/${p.dirname(inputId.path)}';
return p.join(generatedOutputDirectory, inputId.package, p.dirname(inputId.path));
}

String getRelativePathFromOutputToInput(AssetId inputId) {
final outputPath = getOuputPathInGeneratedOutputDirectory(inputId);
return p.relative(inputId.path, from: outputPath);
return normalizeAssetPath(p.relative(inputId.path, from: outputPath));
}

/// Asset paths always have forward slashes regardless of platform.
String normalizeAssetPath(String path) => path.replaceAll(r'\', '/');
9 changes: 8 additions & 1 deletion monarch/lib/src/builders/main_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'dart:async';
import 'package:build/build.dart';
import 'package:glob/glob.dart';
import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as p;

import 'builder_helper.dart';

class MainBuilder implements Builder {
@override
Expand Down Expand Up @@ -60,7 +63,11 @@ class MainBuilder implements Builder {
Iterable<String> _getImportStatements(Map<String, AssetId> map) {
return map.entries.map((item) {
final libraryPrefix = item.key;
return "import '../${item.value.path}' as $libraryPrefix;";
final targetPath = item.value.path;

final relativePath =
normalizeAssetPath(p.relative(targetPath, from: 'lib'));
return "import r'$relativePath' as $libraryPrefix;";
});
}

Expand Down
2 changes: 1 addition & 1 deletion monarch/lib/src/builders/meta_stories_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MetaStoriesGenerator extends Generator {
List<String> storiesNames, Map<String, String> storiesMap) {
return '''
import 'package:monarch/monarch.dart';
import '$pathToStoriesFile';
import r'$pathToStoriesFile';

const metaStories = MetaStories('${storiesAssetId.package}', '${storiesAssetId.path}', [${storiesNames.join(', ')}], $storiesMap);

Expand Down
2 changes: 1 addition & 1 deletion monarch/lib/src/builders/meta_themes_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Element name: ${element.name}
String pathToThemeFile, List<String> metaThemeExpressions) {
return '''
import 'package:monarch/monarch.dart';
import '$pathToThemeFile';
import r'$pathToThemeFile';

final metaThemeItems = [
${metaThemeExpressions.join(', ')}
Expand Down
18 changes: 15 additions & 3 deletions monarch/lib/src/monarch_app/active_locale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ActiveLocale with Log {
}
}

void resetActiveLocale() {
_activeLocale = null;
log.fine('active locale reset');
}

void assertIsLoaded() {
if (locale == null) {
throw StateError('Expected activeLocale to be set');
Expand All @@ -54,16 +59,23 @@ class ActiveLocale with Log {
}

void setActiveLocaleTag(String localeTag) {
setActiveLocale(parseLocale(localeTag));
if (localeTag == 'System Locale') {
// As of 2020-10-08, the platform apps send 'System Locale' when there aren't
// any user-defined locales, in which case the StoryApp will create a MaterialApp
// without any localization data. In this case, Flutter's default behavior is to
// use the only supported locale which is en-US.
resetActiveLocale();
}
else {
setActiveLocale(parseLocale(localeTag));
}
}

void close() {
_loadingStatusStreamController.close();
}
}

const defaultLocale = Locale('en', 'US');

Locale parseLocale(String localeTag) {
ArgumentError.checkNotNull(localeTag, 'localeTag');
if (localeTag.isEmpty) {
Expand Down
3 changes: 3 additions & 0 deletions monarch/lib/src/monarch_app/stories_errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ String _getActiveStoryErrorMessage() {
return 'There was no active story selected.';
} else {
final metaStories = _monarchData.metaStoriesMap[activeStoryId.pathKey];
if (metaStories == null) {
return 'Unexpected - Could not find meta stories for ${activeStoryId.pathKey}';
}
return '''
The relevant story is:
${metaStories.path} > ${activeStoryId.name}''';
Expand Down