1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'base/file_system.dart';
6import 'base/utils.dart';
7import 'platform_plugins.dart';
8import 'project.dart';
9
10/// Represents a CMake-based sub-project.
11///
12/// This defines interfaces common to Windows and Linux projects.
13abstract class CmakeBasedProject {
14 /// The parent of this project.
15 FlutterProject get parent;
16
17 /// Whether the subproject (either Windows or Linux) exists in the Flutter project.
18 bool existsSync();
19
20 /// The native project CMake specification.
21 File get cmakeFile;
22
23 /// Contains definitions for the Flutter library and the tool.
24 File get managedCmakeFile;
25
26 /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
27 /// the build.
28 File get generatedCmakeConfigFile;
29
30 /// Included CMake with rules and variables for plugin builds.
31 File get generatedPluginCmakeFile;
32
33 /// The directory to write plugin symlinks.
34 Directory get pluginSymlinkDirectory;
35}
36
37/// The Windows sub project.
38class WindowsProject extends FlutterProjectPlatform implements CmakeBasedProject {
39 WindowsProject.fromFlutter(this.parent);
40
41 @override
42 final FlutterProject parent;
43
44 @override
45 String get pluginConfigKey => WindowsPlugin.kConfigKey;
46
47 String get _childDirectory => 'windows';
48
49 @override
50 bool existsSync() => _editableDirectory.existsSync() && cmakeFile.existsSync();
51
52 @override
53 File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
54
55 @override
56 File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
57
58 @override
59 File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
60
61 @override
62 File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
63
64 /// The native entrypoint's CMake specification.
65 File get runnerCmakeFile => runnerDirectory.childFile('CMakeLists.txt');
66
67 /// The native entrypoint's file that adds Flutter to the window.
68 File get runnerFlutterWindowFile => runnerDirectory.childFile('flutter_window.cpp');
69
70 /// The native entrypoint's resource file. Used to configure things
71 /// like the application icon, name, and version.
72 File get runnerResourceFile => runnerDirectory.childFile('Runner.rc');
73
74 @override
75 Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');
76
77 Directory get _editableDirectory => parent.directory.childDirectory(_childDirectory);
78
79 /// The directory in the project that is managed by Flutter. As much as
80 /// possible, files that are edited by Flutter tooling after initial project
81 /// creation should live here.
82 Directory get managedDirectory => _editableDirectory.childDirectory('flutter');
83
84 /// The subdirectory of [managedDirectory] that contains files that are
85 /// generated on the fly. All generated files that are not intended to be
86 /// checked in should live here.
87 Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
88
89 /// The directory in the project that is owned by the app. As much as
90 /// possible, Flutter tooling should not edit files in this directory after
91 /// initial project creation.
92 Directory get runnerDirectory => _editableDirectory.childDirectory('runner');
93
94 Future<void> ensureReadyForPlatformSpecificTooling() async {}
95}
96
97/// The Linux sub project.
98class LinuxProject extends FlutterProjectPlatform implements CmakeBasedProject {
99 LinuxProject.fromFlutter(this.parent);
100
101 @override
102 final FlutterProject parent;
103
104 @override
105 String get pluginConfigKey => LinuxPlugin.kConfigKey;
106
107 static final _applicationIdPattern = RegExp(
108 r'''^\s*set\s*\(\s*APPLICATION_ID\s*"(.*)"\s*\)\s*$''',
109 );
110
111 Directory get _editableDirectory => parent.directory.childDirectory('linux');
112
113 /// The directory in the project that is managed by Flutter. As much as
114 /// possible, files that are edited by Flutter tooling after initial project
115 /// creation should live here.
116 Directory get managedDirectory => _editableDirectory.childDirectory('flutter');
117
118 /// The subdirectory of [managedDirectory] that contains files that are
119 /// generated on the fly. All generated files that are not intended to be
120 /// checked in should live here.
121 Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
122
123 @override
124 bool existsSync() => _editableDirectory.existsSync();
125
126 @override
127 File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
128
129 @override
130 File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
131
132 @override
133 File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
134
135 @override
136 File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
137
138 @override
139 Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');
140
141 Future<void> ensureReadyForPlatformSpecificTooling() async {}
142
143 String? get applicationId {
144 return firstMatchInFile(cmakeFile, _applicationIdPattern)?.group(1);
145 }
146}
147