Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ModelParser {
fileName: outFileName,
fields: fields,
indexes: indexes,
subDirParts: protocolSource.protocolRootPathParts,
subDirParts: protocolSource.subDirPathParts,
documentation: classDocumentation,
isException: documentTypeName == Keyword.exceptionType,
serverOnly: serverOnly,
Expand Down Expand Up @@ -106,7 +106,7 @@ class ModelParser {
values: values,
serialized: serializeAs,
documentation: enumDocumentation,
subDirParts: protocolSource.protocolRootPathParts,
subDirParts: protocolSource.subDirPathParts,
serverOnly: serverOnly,
type: enumType,
);
Expand Down
59 changes: 48 additions & 11 deletions tools/serverpod_cli/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,28 @@ class ServerpodModulesNotFoundException implements Exception {
String toString() => message;
}

abstract interface class ModelLoadConfig {
/// Path parts to the lib/src/protocol directory of the server package.
List<String> get protocolSourcePathParts;

/// Path parts to the lib/src/models directory of the server package.
List<String> get modelSourcePathParts;

/// Path parts to the lib/src folder of the server package.
List<String> get srcSourcePathParts;

/// Path parts to the lib folder of the server package.
List<String> get libSourcePathParts;

/// Relative path parts to the model directory
List<String> get relativeModelSourcePathParts;

/// Relative path parts to the protocol directory
List<String> get relativeProtocolSourcePathParts;
}

/// The configuration of the generation and analyzing process.
class GeneratorConfig {
class GeneratorConfig implements ModelLoadConfig {
const GeneratorConfig({
required this.name,
required this.type,
Expand Down Expand Up @@ -97,22 +117,25 @@ class GeneratorConfig {
/// Might be relative.
final List<String> serverPackageDirectoryPathParts;

/// Path parts to the lib folder of the server package.
@override
List<String> get libSourcePathParts =>
[...serverPackageDirectoryPathParts, 'lib'];

/// Relative path parts to the protocol directory
@override
List<String> get srcSourcePathParts => [...libSourcePathParts, 'src'];

@override
List<String> get relativeProtocolSourcePathParts =>
['lib', 'src', 'protocol'];

/// Path parts to the protocol directory of the server package.
@override
List<String> get protocolSourcePathParts =>
[...serverPackageDirectoryPathParts, ...relativeProtocolSourcePathParts];

/// Relative path parts to the model directory
@override
List<String> get relativeModelSourcePathParts => ['lib', 'src', 'models'];

/// Path parts to the model directory of the server package.
@override
List<String> get modelSourcePathParts =>
[...serverPackageDirectoryPathParts, ...relativeModelSourcePathParts];

Expand Down Expand Up @@ -439,7 +462,7 @@ generatedServerModel: ${p.joinAll(generatedServeModelPathParts)}
}

/// Describes the configuration of a Serverpod module a package depends on.
class ModuleConfig {
class ModuleConfig implements ModelLoadConfig {
PackageType type;

/// The user defined nickname of the module.
Expand All @@ -458,13 +481,27 @@ class ModuleConfig {
/// Might be relative.
final List<String> serverPackageDirectoryPathParts;

/// Path parts to the protocol directory of the server package.
@override
List<String> get libSourcePathParts =>
[...serverPackageDirectoryPathParts, 'lib'];

@override
List<String> get srcSourcePathParts => [...libSourcePathParts, 'src'];

@override
List<String> get relativeProtocolSourcePathParts =>
['lib', 'src', 'protocol'];

@override
List<String> get protocolSourcePathParts =>
[...serverPackageDirectoryPathParts, 'lib', 'src', 'protocol'];
[...serverPackageDirectoryPathParts, ...relativeProtocolSourcePathParts];

/// Path parts to the model directory of the server package.
@override
List<String> get relativeModelSourcePathParts => ['lib', 'src', 'models'];

@override
List<String> get modelSourcePathParts =>
[...serverPackageDirectoryPathParts, 'lib', 'src', 'models'];
[...serverPackageDirectoryPathParts, ...relativeModelSourcePathParts];

/// The migration versions of the module.
List<String> migrationVersions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ Future<bool> performGenerateContinuously({
'Initial code generation complete. Listening for changes.',
);

var modelSourcePath = p.joinAll(config.modelSourcePathParts);
var protocolSourcePath = p.joinAll(config.protocolSourcePathParts);

Timer? debouncedGenerate;
await for (WatchEvent event in watchers) {
log.debug('File changed: $event');
Expand All @@ -43,8 +40,7 @@ Future<bool> performGenerateContinuously({

if (ModelHelper.isModelFile(
event.path,
modelSourcePath,
protocolSourcePath,
loadConfig: config,
)) {
shouldGenerate = true;
var modelUri = Uri.parse(p.absolute(event.path));
Expand Down
75 changes: 33 additions & 42 deletions tools/serverpod_cli/lib/src/util/model_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class ModelSource {
String moduleAlias;
String yaml;
Uri yamlSourceUri;
List<String> protocolRootPathParts;
List<String> subDirPathParts;

ModelSource(
this.moduleAlias,
this.yaml,
this.yamlSourceUri,
this.protocolRootPathParts,
this.subDirPathParts,
);
}

Expand All @@ -33,40 +33,32 @@ class ModelHelper {
) async {
var modelSources = <ModelSource>[];

var relativeModelSourcePath = joinAll(config.relativeModelSourcePathParts);
var relativeProtocolSourcePath =
joinAll(config.relativeProtocolSourcePathParts);

var modelSource = await _loadYamlModelsFromDisk(
defaultModuleAlias,
_absolutePathParts(config.modelSourcePathParts),
relativeModelSourcePath: relativeModelSourcePath,
relativeProtocolSourcePath: relativeProtocolSourcePath,
loadConfig: config,
);
modelSources.addAll(modelSource);

modelSource = await _loadYamlModelsFromDisk(
defaultModuleAlias,
_absolutePathParts(config.protocolSourcePathParts),
relativeModelSourcePath: relativeModelSourcePath,
relativeProtocolSourcePath: relativeProtocolSourcePath,
loadConfig: config,
);
modelSources.addAll(modelSource);

for (var module in config.modulesDependent) {
modelSource = await _loadYamlModelsFromDisk(
module.nickname,
module.modelSourcePathParts,
relativeModelSourcePath: relativeModelSourcePath,
relativeProtocolSourcePath: relativeProtocolSourcePath,
loadConfig: module,
);
modelSources.addAll(modelSource);

modelSource = await _loadYamlModelsFromDisk(
module.nickname,
module.protocolSourcePathParts,
relativeModelSourcePath: relativeModelSourcePath,
relativeProtocolSourcePath: relativeProtocolSourcePath,
loadConfig: module,
);
modelSources.addAll(modelSource);
}
Expand All @@ -86,14 +78,9 @@ class ModelHelper {
static Future<List<ModelSource>> _loadYamlModelsFromDisk(
String moduleAlias,
List<String> pathParts, {
required String relativeModelSourcePath,
required String relativeProtocolSourcePath,
required ModelLoadConfig loadConfig,
}) async {
var files = await _loadAllModelFiles(
pathParts,
relativeModelSourcePath: relativeModelSourcePath,
relativeProtocolSourcePath: relativeProtocolSourcePath,
);
var files = await _loadAllModelFiles(pathParts, loadConfig: loadConfig);

List<ModelSource> sources = [];
for (var model in files) {
Expand All @@ -103,22 +90,23 @@ class ModelHelper {
moduleAlias,
yaml,
model.uri,
extractPathFromModelRoot(pathParts, model.uri),
_extractPathFromModelRoot(pathParts, model.uri),
));
}

return sources;
}

static bool isModelFile(
String path,
String modelSourcePath,
String protocolSourcePath,
) {
var hasValidPath = path.containsAny([
modelSourcePath,
protocolSourcePath,
]);
String path, {
required ModelLoadConfig loadConfig,
}) {
var allowedModelPaths = [
joinAll(loadConfig.relativeModelSourcePathParts),
joinAll(loadConfig.relativeProtocolSourcePathParts),
];

var hasValidPath = path.containsAny(allowedModelPaths);

var hasValidExtension = modelFileExtensions.any(
(ext) => path.endsWith(ext),
Expand All @@ -129,8 +117,7 @@ class ModelHelper {

static Future<Iterable<File>> _loadAllModelFiles(
List<String> absolutePathParts, {
required String relativeModelSourcePath,
required String relativeProtocolSourcePath,
required ModelLoadConfig loadConfig,
}) async {
List<FileSystemEntity> modelSourceFileList = [];

Expand All @@ -151,24 +138,28 @@ class ModelHelper {

return modelSourceFileList.whereType<File>().where((file) => isModelFile(
file.path,
relativeModelSourcePath,
relativeProtocolSourcePath,
loadConfig: loadConfig,
));
}

static List<String> extractPathFromConfig(GeneratorConfig config, Uri uri) {
if (isWithin(joinAll(config.protocolSourcePathParts), uri.path)) {
return extractPathFromModelRoot(config.protocolSourcePathParts, uri);
}

if (isWithin(joinAll(config.modelSourcePathParts), uri.path)) {
return extractPathFromModelRoot(config.modelSourcePathParts, uri);
static List<String> extractPathFromConfig(ModelLoadConfig config, Uri uri) {
List<List<String>> paths = [
config.protocolSourcePathParts,
config.modelSourcePathParts,
config.srcSourcePathParts,
config.libSourcePathParts,
];

for (var path in paths) {
if (isWithin(joinAll(path), uri.path)) {
return _extractPathFromModelRoot(path, uri);
}
}

return split(uri.path);
}

static List<String> extractPathFromModelRoot(
static List<String> _extractPathFromModelRoot(
List<String> pathParts,
Uri fileUri,
) {
Expand Down
Loading