( sdks: Sdks, logger: Logger, folderUri: Uri, includeDatesAndFullPaths = true, existsSync: (itemPath: string) => boolean = fs.existsSync, readFileSync: (itemPath: string) => string = (p) => fs.readFileSync(p, "utf8").toString(), mtimeSync: (itemPath: string) => Date = (p) => fs.statSync(p).mtime, )
| 113 | } |
| 114 | |
| 115 | function getPubPackageStatus( |
| 116 | sdks: Sdks, |
| 117 | logger: Logger, |
| 118 | folderUri: Uri, |
| 119 | includeDatesAndFullPaths = true, |
| 120 | existsSync: (itemPath: string) => boolean = fs.existsSync, |
| 121 | readFileSync: (itemPath: string) => string = (p) => fs.readFileSync(p, "utf8").toString(), |
| 122 | mtimeSync: (itemPath: string) => Date = (p) => fs.statSync(p).mtime, |
| 123 | ): PubPackageStatus { |
| 124 | const folder = fsPath(folderUri); |
| 125 | const pubspecPath = path.join(folder, "pubspec.yaml"); |
| 126 | const pubspecLockPath = path.join(folder, "pubspec.lock"); |
| 127 | const packageMapPath = path.join(folder, ".dart_tool", "package_config.json"); |
| 128 | if (!folder || !existsSync(pubspecPath)) |
| 129 | return { folderUri, pubRequired: false, reason: "Folder or pubspec do not exist", workspace: "NONE" }; |
| 130 | |
| 131 | const isValid = isValidPubGetTarget(folderUri); |
| 132 | if (!isValid.valid) |
| 133 | return { folderUri, pubRequired: false, reason: isValid.reason, workspace: "NONE" }; |
| 134 | |
| 135 | const pubspecContent = readFileSync(pubspecPath); |
| 136 | const hasDependencies = pubspecHasDependenciesRegex.test(pubspecContent); |
| 137 | const isWorkspaceProject = pubspecIsWorkspaceProjectRegex.test(pubspecContent); |
| 138 | const isWorkspaceRoot = pubspecIsWorkspaceRootRegex.test(pubspecContent); |
| 139 | |
| 140 | const result: Partial<PubPackageStatus> & { folderUri: Uri, workspace: string } = { folderUri, workspace: isWorkspaceRoot ? "ROOT" : isWorkspaceProject ? "PROJECT" : "NONE" }; |
| 141 | |
| 142 | // If we don't appear to have deps listed in pubspec, then no point prompting. |
| 143 | if (!isWorkspaceRoot && !hasDependencies) |
| 144 | return { ...result, pubRequired: false, reason: "Pubspec does not contain any dependencies" }; |
| 145 | |
| 146 | // If we are part of a pub workspace, we don't do anything, it's handled by the root. |
| 147 | if (isWorkspaceProject) |
| 148 | return { ...result, pubRequired: false, reason: "The project is part of a Pub workspace" }; |
| 149 | |
| 150 | // If we don't have package_config, we probably need running. |
| 151 | if (!existsSync(packageMapPath)) |
| 152 | return { ...result, pubRequired: "GET", reason: "The .dart_tool/package_config.json file is missing" }; |
| 153 | |
| 154 | // If the Dart SDK version has upgraded by more than just a patch, we should |
| 155 | // prefer upgrade. |
| 156 | const lastUsedSdkVersion = getPubGeneratorVersion(logger, packageMapPath, existsSync, readFileSync); |
| 157 | const currentSdkVersion = sdks.dartVersion; |
| 158 | if (lastUsedSdkVersion && currentSdkVersion) { |
| 159 | const lastUsedSdkMajorMinor = `${semver.major(lastUsedSdkVersion)}.${semver.minor(lastUsedSdkVersion)}.0`; |
| 160 | const currentSdkMajorMinor = `${semver.major(currentSdkVersion)}.${semver.minor(currentSdkVersion)}.0`; |
| 161 | |
| 162 | logger.info(`Version last used for Pub is ${lastUsedSdkVersion} (${lastUsedSdkMajorMinor}), current is ${currentSdkVersion} (${currentSdkMajorMinor})`); |
| 163 | // For an SDK upgrade, we want to encourage upgrading. |
| 164 | if (semver.gt(currentSdkMajorMinor, lastUsedSdkMajorMinor)) |
| 165 | return { ...result, pubRequired: "UPGRADE", reason: `The current SDK version (${currentSdkVersion}) is newer than the one last used to run "pub get" (${lastUsedSdkVersion})` }; |
| 166 | // For a downgrade, Pub Get is enough to fix. |
| 167 | else if (semver.lt(currentSdkMajorMinor, lastUsedSdkMajorMinor)) |
| 168 | return { ...result, pubRequired: "GET", reason: `The current SDK version (${currentSdkVersion}) is older than the one last used to run "pub get" (${lastUsedSdkVersion})` }; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | // Helpers for Date/Paths so that output is complete for production, but simplified for tests. |
no test coverage detected