-
Notifications
You must be signed in to change notification settings - Fork 71
Fix #1180 Add the ability to build filters completely locally #1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a8ac982
dce899d
395bd2f
b656361
a1af0ab
53c6a5c
7af7209
576f7ef
a12947d
54a6df8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add tests for new flag combinations |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,23 @@ import { | |
| FOLDER_WITH_NEW_FILTERS, | ||
| FOLDER_WITH_OLD_FILTERS, | ||
| } from './constants.js'; | ||
| import { stripGeneratedMetaFromDir } from './strip-generated-meta.ts'; | ||
| import { findFiles } from '../utils/find_files.js'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| /** | ||
| * Parse command-cli parameters -i|--include and -s|--skip | ||
| * Parse command-line parameters -i|--include, -s|--skip, --use-cache, --generate-cache, | ||
| * --no-patches-prepare, --strip-generated-meta | ||
| */ | ||
| let includedFilterIDs = []; | ||
| let excludedFilterIDs = []; | ||
| let rawReportPath = ''; | ||
| let useCache = false; | ||
| let generateCache = false; | ||
| let noPatchesPrepare = false; | ||
| let stripGeneratedMeta = false; | ||
|
|
||
| const args = process.argv.slice(2); | ||
| args.forEach((val) => { | ||
|
|
@@ -40,26 +47,105 @@ args.forEach((val) => { | |
| if (val.startsWith('--report=')) { | ||
| rawReportPath = val.slice(val.indexOf('=') + 1).trim(); | ||
| } | ||
|
|
||
| if (val === '--use-cache') { | ||
| useCache = true; | ||
| } | ||
|
|
||
| if (val === '--generate-cache') { | ||
|
Alex-302 marked this conversation as resolved.
|
||
| generateCache = true; | ||
| } | ||
|
|
||
| if (val === '--no-patches-prepare') { | ||
| noPatchesPrepare = true; | ||
| } | ||
|
|
||
| if (val === '--strip-generated-meta') { | ||
| stripGeneratedMeta = true; | ||
| } | ||
| }); | ||
|
|
||
| if (useCache && generateCache) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error: --use-cache and --generate-cache are mutually exclusive.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| /** | ||
| * Set all relative paths needed for compiler | ||
| */ | ||
| const filtersDir = path.join(__dirname, '../../filters'); | ||
| const logPath = path.join(__dirname, '../../log.txt'); | ||
| const platformsPath = path.join(__dirname, '../..', FOLDER_WITH_NEW_FILTERS); | ||
| const copyPlatformsPath = path.join(__dirname, '../..', FOLDER_WITH_OLD_FILTERS); | ||
| const cachedFiltersDir = path.join(__dirname, '../../temp/filters_cached'); | ||
|
|
||
| const reportPath = rawReportPath !== '' | ||
| // report-adguard.txt OR report-third-party.txt | ||
| ? path.join(__dirname, '../..', rawReportPath) | ||
| // report_partial_DD-MM-YYYY_HH-MM-SS.txt | ||
| : path.join(__dirname, '../..', `report_partial_${formatDate(new Date())}.txt`); | ||
|
|
||
| const SHADOW_TEMPLATE_CONTENT = '@include "./filter.txt"\n'; | ||
|
|
||
| /** | ||
| * Prepare a temporary copy of the filters directory with shadow templates. | ||
| * | ||
| * Copies `filters/` → `temp/filters_cached/`, then replaces the content of every | ||
| * `template.txt` with a single-line local include pointing to the cached `filter.txt`. | ||
| * Validates that every filter directory with a `template.txt` also has a `filter.txt`. | ||
| * | ||
| * @returns {Promise<void>} | ||
| */ | ||
| const prepareCachedFiltersDir = async () => { | ||
| // Remove stale copy if exists | ||
| if (fs.existsSync(cachedFiltersDir)) { | ||
| await fs.promises.rm(cachedFiltersDir, { recursive: true }); | ||
| } | ||
|
|
||
| // Full recursive copy | ||
| await fs.promises.cp(filtersDir, cachedFiltersDir, { recursive: true }); | ||
|
|
||
| // Find all directories containing template.txt and replace with shadow templates | ||
| const templatePaths = await findFiles(cachedFiltersDir, (p) => path.basename(p) === 'template.txt'); | ||
|
|
||
| await Promise.all(templatePaths.map(async (templatePath) => { | ||
| const dir = path.dirname(templatePath); | ||
| const filterTxtPath = path.join(dir, 'filter.txt'); | ||
|
|
||
| if (!fs.existsSync(filterTxtPath)) { | ||
| throw new Error( | ||
| `--use-cache: missing filter.txt in ${path.relative(cachedFiltersDir, dir)}. ` | ||
| + 'Run "yarn generate-cache" first to generate cached filter files.', | ||
| ); | ||
| } | ||
|
|
||
| await fs.promises.writeFile(templatePath, SHADOW_TEMPLATE_CONTENT, 'utf8'); | ||
| })); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`Prepared cached filters directory with ${templatePaths.length} shadow templates.`); | ||
| }; | ||
|
|
||
| /** | ||
| * Compiler entry point. | ||
| */ | ||
| const buildFilters = async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This introduces several new execution paths and flag interactions ( Could we extract the mode selection and argument handling into smaller helpers and add tests for the new combinations? The two blocker scenarios here are exactly the kind of regressions that are easy to miss without targeted coverage. |
||
| // When --generate-cache we only need to compile filters (which updates filter.txt), | ||
| // skip platform generation, patches preparation, and temp/platforms copying. | ||
| if (generateCache) { | ||
| await compile( | ||
| filtersDir, | ||
| logPath, | ||
| reportPath, | ||
| null, // null ⇒ generate() inside compiler returns early, no platform files | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Clean temporary folder | ||
| if (fs.existsSync(copyPlatformsPath)) { | ||
| await fs.promises.rm(copyPlatformsPath, { recursive: true }); | ||
|
|
@@ -70,27 +156,49 @@ const buildFilters = async () => { | |
| let initialRun = false; | ||
| if (!fs.existsSync(platformsPath)) { | ||
| initialRun = true; | ||
| } else { | ||
| } else if (!noPatchesPrepare) { | ||
| // Make copy for future patches generation | ||
| await fs.promises.cp(platformsPath, copyPlatformsPath, { recursive: true }); | ||
| } | ||
|
|
||
| await compile( | ||
| filtersDir, | ||
| logPath, | ||
| reportPath, | ||
| platformsPath, | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| // Determine which filtersDir to pass to the compiler | ||
| const effectiveFiltersDir = useCache ? cachedFiltersDir : filtersDir; | ||
|
|
||
| if (useCache) { | ||
| await prepareCachedFiltersDir(); | ||
| } | ||
|
|
||
| try { | ||
| await compile( | ||
| effectiveFiltersDir, | ||
| logPath, | ||
| reportPath, | ||
| platformsPath, | ||
| includedFilterIDs, | ||
| excludedFilterIDs, | ||
| CUSTOM_PLATFORMS_CONFIG, | ||
| ); | ||
| } finally { | ||
| // Clean up temp filters copy | ||
| if (useCache && fs.existsSync(cachedFiltersDir)) { | ||
| await fs.promises.rm(cachedFiltersDir, { recursive: true }); | ||
| } | ||
| } | ||
|
|
||
| // For the very first run, we should copy the built platforms into | ||
| // the temp folder to create the first empty patches for future versions | ||
| if (initialRun) { | ||
| if (initialRun && !noPatchesPrepare) { | ||
| // Make copy for future patches generation | ||
| await fs.promises.cp(platformsPath, copyPlatformsPath, { recursive: true }); | ||
| } | ||
|
|
||
| // Strip generated metadata (Checksum, Diff-Path, TimeUpdated, Version) | ||
| // from compiled filter files so they don't pollute diff comparisons. | ||
| if (stripGeneratedMeta) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This strips metadata only from the new That means a later |
||
| const count = await stripGeneratedMetaFromDir(platformsPath); | ||
| // eslint-disable-next-line no-console | ||
| console.log(`Stripped generated meta from ${count} file(s).`); | ||
| } | ||
| }; | ||
|
|
||
| buildFilters(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarn strip-generated-metapoints toscripts/build/strip-generated-meta.ts, but this file only exportsstripGeneratedMetaFromDir()and never invokes it.I verified locally that the command exits successfully without touching the generated files. Please add a CLI entrypoint here, or point the npm script to a small wrapper that actually calls the function.