1+ /*-----------------------------------------------------------------------------------------------
2+ * Copyright (c) Red Hat, Inc. All rights reserved.
3+ * Licensed under the MIT License. See LICENSE file in the project root for license information.
4+ *-----------------------------------------------------------------------------------------------*/
5+
6+ 'use strict' ;
7+
8+ import hasha = require( 'hasha' ) ;
9+ import mkdirp = require( 'mkdirp' ) ;
10+ import fs = require( 'fs-extra' ) ;
11+ import path = require( "path" ) ;
12+ import cp = require( 'child_process' ) ;
13+ import configData = require( '../src/tools.json' ) ;
14+ import os = require( 'os' ) ;
15+ import { DownloadUtil } from '../src/util/download' ;
16+
17+ async function verifyTools ( ) {
18+ for ( const key in configData ) {
19+ for ( const OS in configData [ key ] . platform ) {
20+ const targetFolder = path . resolve ( os . tmpdir ( ) , OS ) ;
21+ await downloadFileAndCreateSha256 ( targetFolder , configData [ key ] . platform [ OS ] . dlFileName , configData [ key ] . platform [ OS ] . url , configData [ key ] . platform [ OS ] . sha256sum ) ;
22+ }
23+ }
24+ }
25+
26+ async function downloadFileAndCreateSha256 ( targetFolder : string , fileName : string , reqURL : string , sha256sum : string ) {
27+ if ( ! fs . existsSync ( targetFolder ) ) {
28+ await mkdirp . sync ( targetFolder ) ;
29+ }
30+ const currentFile = path . join ( targetFolder , fileName ) ;
31+ console . log ( `${ currentFile } download started from ${ reqURL } ` )
32+ await DownloadUtil . downloadFile ( reqURL , currentFile , ( current ) => console . log ( current + '%' ) ) ;
33+ const currentSHA256 = await hasha . fromFile ( currentFile , { algorithm : 'sha256' } ) ; ;
34+ if ( currentSHA256 === sha256sum ) {
35+ console . log ( `[INFO] ${ currentFile } is downloaded and sha256 is correct` ) ;
36+ } else {
37+ throw Error ( `${ currentFile } is downloaded and sha256 is not correct` ) ;
38+ }
39+ }
40+
41+ const fileCheckRegex = / \w * t o o l s .j s o n / ;
42+ cp . exec ( 'git diff --name-only master..HEAD' , async ( error , stdout , stderr ) => {
43+ if ( error ) {
44+ throw error ;
45+ }
46+ console . log ( 'The changed files:' ) ;
47+ console . log ( stdout ) ;
48+ if ( fileCheckRegex . test ( stdout ) ) {
49+ console . log ( 'tools.json is changed, starting download verification' )
50+ await verifyTools ( ) ;
51+ } else {
52+ console . log ( 'tools.json is not changed, skipping download verification' )
53+ }
54+ } ) ;
0 commit comments