-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathLib.hs
1888 lines (1715 loc) · 76.8 KB
/
Lib.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Lib (module Lib
, Types.RomeVersion
, Utils.romeVersionToString
)
where
import Caches.Common
import Caches.Local.Downloading
import Caches.Local.Probing
import Caches.Local.Uploading
import Caches.S3.Downloading
import Caches.S3.Probing
import Caches.S3.Uploading
import Engine.Downloading
import Engine.Probing
import Engine.Uploading
import Configuration
import Control.Applicative ((<|>))
import Control.Concurrent.Async.Lifted.Safe (mapConcurrently_, mapConcurrently, concurrently_)
import Control.Lens hiding (List)
import Control.Monad
import Control.Monad.Catch
import Control.Monad.Except
import Control.Monad.Reader (ReaderT, ask, runReaderT)
import Control.Monad.Trans.Maybe (exceptToMaybeT, runMaybeT)
import qualified Data.ByteString.Char8 as BS (pack)
import qualified Data.ByteString.Lazy as LBS
import Data.Yaml (encodeFile)
import Data.IORef (newIORef)
import Data.Carthage.Cartfile
import Data.Carthage.TargetPlatform
import Data.Either.Extra (maybeToEither, eitherToMaybe, mapLeft)
import Data.Maybe (fromMaybe, maybe)
import Data.Monoid ((<>))
import Data.Romefile
import qualified Data.Map.Strict as M (empty)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T (encodeUtf8)
import qualified Network.AWS as AWS
import qualified Network.AWS.Auth as AWS (fromEnv)
import qualified Network.AWS.Env as AWS (Env (..), retryConnectionFailure)
import qualified Network.AWS.Data as AWS (fromText)
import qualified Network.AWS.Data.Sensitive as AWS (Sensitive (..))
import qualified Network.AWS.S3 as S3
import qualified Network.AWS.STS.AssumeRole as STS (assumeRole, arrsCredentials)
import qualified Network.AWS.Utils as AWS
import qualified Network.HTTP.Conduit as Conduit
import Network.URL
import System.Directory
import System.Environment
import System.FilePath
import Types
import Types.Commands as Commands
import Utils
import Xcode.DWARF
s3EndpointOverride :: URL -> AWS.Service
s3EndpointOverride (URL (Absolute h) _ _) =
let isSecure = secure h
host' = host h
port' = port h <|> if isSecure then Just 443 else Nothing
in AWS.setEndpoint isSecure
(BS.pack host')
(maybe 9000 fromInteger port')
S3.s3
s3EndpointOverride _ = S3.s3
-- | Tries to get authentication details and region to perform
-- | requests to AWS.
-- | The `AWS_PROFILE` is read from the environment
-- | or falls back to `default`.
-- | The `AWS_REGION` is first read from the environment, if not found
-- | it is read from `~/.aws/config` based on the profile discovered in the previous step.
-- | The `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` are first
-- | read from the environment. If not found, then the `~/.aws/credentials`
-- | file is read. If `source_profile` key is present the reading of the
-- | authentication details happens from this profile rather then the `AWS_PROFILE`.
-- | Finally, if `role_arn` is specified, the credentials gathered up to now are used
-- | to obtain new credentials with STS escalated to `role_arn`.
getAWSEnv :: (MonadIO m, MonadCatch m) => ExceptT String m AWS.Env
getAWSEnv = do
region <- discoverRegion
endpointURL <- runMaybeT . exceptToMaybeT $ discoverEndpoint
profile <- T.pack . fromMaybe "default" <$> liftIO
(lookupEnv (T.unpack "AWS_PROFILE"))
credentials <-
runExceptT $ (AWS.credentialsFromFile =<< getAWSCredentialsFilePath) `catch` \(e :: IOError) -> ExceptT . return . Left . show $ e
config <-
runExceptT $ (AWS.configFromFile =<< getAWSConfigFilePath) `catch` \(e :: IOError) -> ExceptT . return . Left . show $ e
(auth, _) <-
AWS.catching AWS._MissingEnvError AWS.fromEnv $ \envError -> either
throwError
(\cred -> do
let finalProfile = fromMaybe
profile
(eitherToMaybe $ AWS.sourceProfileOf profile =<< config)
let
authAndRegion =
(,)
<$> mapLeft
(\e ->
T.unpack envError
++ ". "
++ e
++ " in file ~/.aws/credentials"
)
(AWS.authFromCredentilas finalProfile =<< credentials)
<*> pure (pure region)
liftEither authAndRegion
)
credentials
manager <- liftIO (Conduit.newManager Conduit.tlsManagerSettings)
ref <- liftIO (newIORef Nothing)
let roleARN = eitherToMaybe $ AWS.roleARNOf profile =<< config
let curerntEnv = AWS.Env region
(\_ _ -> pure ())
(AWS.retryConnectionFailure 3)
mempty
manager
ref
auth
case roleARN of
Just role -> newEnvFromRole role curerntEnv
Nothing -> return
$ AWS.configure (maybe S3.s3 s3EndpointOverride endpointURL) curerntEnv
newEnvFromRole :: MonadIO m => T.Text -> AWS.Env -> ExceptT String m AWS.Env
newEnvFromRole roleARN currentEnv = do
assumeRoleResult <-
liftIO
$ AWS.runResourceT
. AWS.runAWS currentEnv
$ AWS.send
$ STS.assumeRole roleARN "rome-cache-operation"
let maybeAuth = AWS.Auth <$> assumeRoleResult ^. STS.arrsCredentials
case maybeAuth of
Nothing ->
throwError
$ "Could not create AWS Auth from STS response: "
++ show assumeRoleResult
Just newAuth -> return $ currentEnv & AWS.envAuth .~ newAuth
getAWSRegion :: (MonadIO m, MonadCatch m) => ExceptT String m AWS.Env
getAWSRegion = do
region <- discoverRegion
endpointURL <- runMaybeT . exceptToMaybeT $ discoverEndpoint
set AWS.envRegion region
<$> ( AWS.newEnv AWS.Discover
<&> AWS.configure (maybe S3.s3 s3EndpointOverride endpointURL)
)
allCacheKeysMissingMessage :: String
allCacheKeysMissingMessage
= "Error: expected at least one of \"local\", \
\\"s3Bucket\" or \"engine\" in the cache definition of your Romefile."
conflictingCachesMessage :: String
conflictingCachesMessage
= "Error: both \"s3Bucket\" and \"engine\" defined. \
\ Rome cannot use both, choose one."
conflictingSkipLocalCacheOptionMessage :: String
conflictingSkipLocalCacheOptionMessage
= "Error: only \"local\" defined as cache \
\in your Romefile, but you have asked Rome to skip \
\this cache."
-- | Runs Rome with a set of `RomeOptions`.
runRomeWithOptions
:: RomeOptions -- ^ The `RomeOptions` to run Rome with.
-> RomeVersion
-> RomeMonad ()
runRomeWithOptions (RomeOptions options romefilePath verbose) romeVersion = do
absoluteRomefilePath <- liftIO $ absolutizePath romefilePath
case options of
Utils _ ->
runUtilsCommand options absoluteRomefilePath verbose romeVersion
_ ->
runUDCCommand options absoluteRomefilePath verbose romeVersion
-- | Runs one of the Utility commands
runUtilsCommand
:: RomeCommand -> FilePath -> Bool -> RomeVersion -> RomeMonad ()
runUtilsCommand command absoluteRomefilePath _ _ =
case command of
Utils _ -> do
romeFileEntries <- getRomefileEntries absoluteRomefilePath
lift $ encodeFile absoluteRomefilePath romeFileEntries
_ -> throwError "Error: Programming Error. Only Utils command supported."
-- | Runs a command containing a `UDCPayload`
runUDCCommand :: RomeCommand -> FilePath -> Bool -> RomeVersion -> RomeMonad ()
runUDCCommand command absoluteRomefilePath verbose romeVersion = do
cartfileEntries <- getCartfileEntries
`catch` \(_ :: IOError) -> ExceptT . return $ Right []
romeFile <- getRomefileEntries absoluteRomefilePath
let ignoreMapEntries = _ignoreMapEntries romeFile
let currentMapEntries = _currentMapEntries romeFile
let repositoryMapEntries = _repositoryMapEntries romeFile
let ignoreFrameworks = concatMap _frameworks ignoreMapEntries
let cInfo = romeFile ^. cacheInfo
let mS3BucketName = S3.BucketName <$> cInfo ^. bucket
mlCacheDir <- liftIO $ traverse absolutizePath $ cInfo ^. localCacheDir
mEnginePath <- liftIO $ traverse absolutizePath $ cInfo ^. enginePath
case command of
Upload (RomeUDCPayload gitRepoNames platforms cachePrefixString skipLocalCache noIgnoreFlag noSkipCurrentFlag concurrentlyFlag)
-> sayVersionWarning romeVersion verbose
*> performWithDefaultFlow
uploadArtifacts
( verbose
, noIgnoreFlag
, skipLocalCache
, noSkipCurrentFlag
, concurrentlyFlag
)
(repositoryMapEntries, ignoreMapEntries, currentMapEntries)
gitRepoNames
cartfileEntries
cachePrefixString
mS3BucketName
mlCacheDir
mEnginePath
platforms
Download (RomeUDCPayload gitRepoNames platforms cachePrefixString skipLocalCache noIgnoreFlag noSkipCurrentFlag concurrentlyFlag)
-> sayVersionWarning romeVersion verbose
*> performWithDefaultFlow
downloadArtifacts
( verbose
, noIgnoreFlag
, skipLocalCache
, noSkipCurrentFlag
, concurrentlyFlag
)
(repositoryMapEntries, ignoreMapEntries, currentMapEntries)
gitRepoNames
cartfileEntries
cachePrefixString
mS3BucketName
mlCacheDir
mEnginePath
platforms
List (RomeListPayload listMode platforms cachePrefixString printFormat noIgnoreFlag noSkipCurrentFlag)
-> do
currentVersion <- deriveCurrentVersion
let
finalRepositoryMapEntries =
if _noIgnore noIgnoreFlag
then
repositoryMapEntries
else
repositoryMapEntries
`filterRomeFileEntriesByPlatforms` ignoreMapEntries
let repositoryMap = toRepositoryMap finalRepositoryMapEntries
let reverseRepositoryMap =
toInvertedRepositoryMap finalRepositoryMapEntries
let finalIgnoreNames =
if _noIgnore noIgnoreFlag then [] else ignoreFrameworks
let derivedFrameworkVersions =
deriveFrameworkNamesAndVersion repositoryMap cartfileEntries
let frameworkVersions =
derivedFrameworkVersions
`filterOutFrameworksAndVersionsIfNotIn` finalIgnoreNames
let cachePrefix = CachePrefix cachePrefixString
let filteredCurrentMapEntries =
currentMapEntries
`filterRomeFileEntriesByPlatforms` ignoreMapEntries
let currentFrameworks =
concatMap (snd . romeFileEntryToTuple) filteredCurrentMapEntries
let currentFrameworkVersions =
map (flip FrameworkVersion currentVersion) currentFrameworks
let currentInvertedMap =
toInvertedRepositoryMap filteredCurrentMapEntries
runReaderT
(listArtifacts
mS3BucketName
mlCacheDir
mEnginePath
listMode
(reverseRepositoryMap <> if _noSkipCurrent noSkipCurrentFlag
then currentInvertedMap
else M.empty
)
(frameworkVersions <> if _noSkipCurrent noSkipCurrentFlag
then
(currentFrameworkVersions
`filterOutFrameworksAndVersionsIfNotIn` finalIgnoreNames
)
else []
)
platforms
printFormat
)
(cachePrefix, SkipLocalCacheFlag False, verbose)
_ ->
throwError
"Error: Programming Error. Only List, Download, Upload commands are supported."
where
sayVersionWarning vers verb = runMaybeT $ exceptToMaybeT $ do
let sayFunc = if verb then sayLnWithTime else sayLn
(uptoDate, latestVersion) <- checkIfRomeLatestVersionIs vers
unless uptoDate
$ sayFunc
$ redControlSequence
<> "*** Please update to the latest Rome version: "
<> romeVersionToString latestVersion
<> ". "
<> "You are currently on: "
<> romeVersionToString vers
<> noColorControlSequence
type FlowFunction = Maybe S3.BucketName -- ^ Just an S3 Bucket name or Nothing
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing
-> Maybe FilePath -- ^ Just the path to the engine or Nothing
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` from which to derive Frameworks, dSYMs and .version files
-> [TargetPlatform] -- ^ A list of `TargetPlatform` to restrict this operation to.
-> ReaderT (CachePrefix, SkipLocalCacheFlag, ConcurrentlyFlag, Bool) RomeMonad ()
-- | Convenience function wrapping the regular sequence of events
-- | in case of Download or Upload commands
performWithDefaultFlow
:: FlowFunction
-> ( Bool {- verbose -}
, NoIgnoreFlag {- noIgnoreFlag -}
, SkipLocalCacheFlag {- skipLocalCache -}
, NoSkipCurrentFlag {- noSkipCurrentFlag -}
, ConcurrentlyFlag
) {- concurrentlyFlag -}
-> ([RomefileEntry] {- repositoryMapEntries -}
, [RomefileEntry] {- ignoreMapEntries -}
, [RomefileEntry]) {- currentMapEntries -}
-> [ProjectName] -- gitRepoNames
-> [CartfileEntry] -- cartfileEntries
-> String -- cachePrefixString
-> Maybe S3.BucketName -- mS3BucketName
-> Maybe String -- mlCacheDir
-> Maybe String -- mEnginePath
-> [TargetPlatform] -- platforms
-> RomeMonad ()
performWithDefaultFlow flowFunc (verbose, noIgnoreFlag, skipLocalCache, noSkipCurrentFlag, concurrentlyFlag) (repositoryMapEntries, ignoreMapEntries, currentMapEntries) gitRepoNames cartfileEntries cachePrefixString mS3BucketName mlCacheDir mEnginePath platforms
= do
let ignoreFrameworks = concatMap _frameworks ignoreMapEntries
let
finalRepositoryMapEntries =
if _noIgnore noIgnoreFlag
then
repositoryMapEntries
else
repositoryMapEntries
`filterRomeFileEntriesByPlatforms` ignoreMapEntries
let repositoryMap = toRepositoryMap finalRepositoryMapEntries
let reverseRepositoryMap =
toInvertedRepositoryMap finalRepositoryMapEntries
let finalIgnoreNames =
if _noIgnore noIgnoreFlag then [] else ignoreFrameworks
if null gitRepoNames
then
let
derivedFrameworkVersions =
deriveFrameworkNamesAndVersion repositoryMap cartfileEntries
cachePrefix = CachePrefix cachePrefixString
in
do
runReaderT
(flowFunc
mS3BucketName
mlCacheDir
mEnginePath
reverseRepositoryMap
(derivedFrameworkVersions
`filterOutFrameworksAndVersionsIfNotIn` finalIgnoreNames
)
platforms
)
(cachePrefix, skipLocalCache, concurrentlyFlag, verbose)
when (_noSkipCurrent noSkipCurrentFlag) $ do
currentVersion <- deriveCurrentVersion
let filteredCurrentMapEntries =
currentMapEntries
`filterRomeFileEntriesByPlatforms` ignoreMapEntries
let currentFrameworks = concatMap (snd . romeFileEntryToTuple)
filteredCurrentMapEntries
let currentFrameworkVersions = map
(flip FrameworkVersion currentVersion)
currentFrameworks
let currentInvertedMap =
toInvertedRepositoryMap filteredCurrentMapEntries
runReaderT
(flowFunc
mS3BucketName
mlCacheDir
mEnginePath
currentInvertedMap
(currentFrameworkVersions
`filterOutFrameworksAndVersionsIfNotIn` finalIgnoreNames
)
platforms
)
(cachePrefix, skipLocalCache, concurrentlyFlag, verbose)
else do
currentVersion <- deriveCurrentVersion
let filteredCurrentMapEntries =
( (\e -> _projectName e `elem` gitRepoNames)
`filter` currentMapEntries
) -- Make sure the command is only run for the mentioned projects
`filterRomeFileEntriesByPlatforms` ignoreMapEntries
let currentFrameworks =
concatMap (snd . romeFileEntryToTuple) filteredCurrentMapEntries
let currentFrameworkVersions =
map (flip FrameworkVersion currentVersion) currentFrameworks
let
derivedFrameworkVersions = deriveFrameworkNamesAndVersion
repositoryMap
(filterCartfileEntriesByGitRepoNames gitRepoNames cartfileEntries)
frameworkVersions =
(derivedFrameworkVersions <> currentFrameworkVersions)
`filterOutFrameworksAndVersionsIfNotIn` finalIgnoreNames
cachePrefix = CachePrefix cachePrefixString
currentInvertedMap =
toInvertedRepositoryMap filteredCurrentMapEntries
runReaderT
(flowFunc mS3BucketName
mlCacheDir
mEnginePath
(reverseRepositoryMap <> currentInvertedMap)
frameworkVersions
platforms
)
(cachePrefix, skipLocalCache, concurrentlyFlag, verbose)
-- | Lists Frameworks in the caches.
listArtifacts
:: Maybe S3.BucketName -- ^ Just an S3 Bucket name or Nothing
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing
-> Maybe FilePath -- ^ Just the path to the engine or Nothing
-> ListMode -- ^ A list mode to execute this operation in.
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` from which to derive Frameworks
-> [TargetPlatform] -- ^ A list of `TargetPlatform` to limit the operation to.
-> PrintFormat -- ^ A format of the string result: text or JSON.
-> ReaderT
(CachePrefix, SkipLocalCacheFlag, Bool)
RomeMonad
()
listArtifacts mS3BucketName mlCacheDir mEnginePath listMode reverseRepositoryMap frameworkVersions platforms format
= do
(_, _, verbose) <- ask
let sayFunc = if verbose then sayLnWithTime else sayLn
repoAvailabilities <- getProjectAvailabilityFromCaches
mS3BucketName
mlCacheDir
mEnginePath
reverseRepositoryMap
frameworkVersions
platforms
if format == Text
then mapM_ sayFunc $ repoLines repoAvailabilities
else sayFunc $ toJSONStr $ ReposJSON
(fmap formattedRepoAvailabilityJSON repoAvailabilities)
where
repoLines repoAvailabilities = filter (not . null)
$ fmap (formattedRepoAvailability listMode) repoAvailabilities
-- | Produces a list of `ProjectAvailability`s for Frameworks
getProjectAvailabilityFromCaches
:: Maybe S3.BucketName -- ^ Just an S3 Bucket name or Nothing
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing
-> Maybe FilePath -- ^ Just the path to the engine or Nothing
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` from which to derive Frameworks, dSYMs and .version files
-> [TargetPlatform] -- ^ A list of `TargetPlatform`s to limit the operation to.
-> ReaderT
(CachePrefix, SkipLocalCacheFlag, Bool)
RomeMonad
[ProjectAvailability]
getProjectAvailabilityFromCaches (Just s3BucketName) _ Nothing reverseRepositoryMap frameworkVersions platforms
= do
env <- lift getAWSEnv
(cachePrefix, _, verbose) <- ask
let readerEnv = (env, cachePrefix, verbose)
availabilities <- liftIO $ runReaderT
(probeS3ForFrameworks s3BucketName
reverseRepositoryMap
frameworkVersions
platforms
)
readerEnv
return $ getMergedGitRepoAvailabilitiesFromFrameworkAvailabilities
reverseRepositoryMap
availabilities
getProjectAvailabilityFromCaches Nothing (Just lCacheDir) Nothing reverseRepositoryMap frameworkVersions platforms
= do
(cachePrefix, SkipLocalCacheFlag skipLocalCache, _) <- ask
when skipLocalCache $ throwError conflictingSkipLocalCacheOptionMessage
availabilities <- probeLocalCacheForFrameworks lCacheDir
cachePrefix
reverseRepositoryMap
frameworkVersions
platforms
return $ getMergedGitRepoAvailabilitiesFromFrameworkAvailabilities
reverseRepositoryMap
availabilities
getProjectAvailabilityFromCaches Nothing _ (Just ePath) reverseRepositoryMap frameworkVersions platforms
= do
(cachePrefix, _, _) <- ask
availabilities <- probeEngineForFrameworks ePath
cachePrefix
reverseRepositoryMap
frameworkVersions
platforms
return $ getMergedGitRepoAvailabilitiesFromFrameworkAvailabilities
reverseRepositoryMap
availabilities
getProjectAvailabilityFromCaches (Just _) _ (Just _) _ _ _ =
throwError conflictingCachesMessage
getProjectAvailabilityFromCaches Nothing Nothing Nothing _ _ _ =
throwError allCacheKeysMissingMessage
-- | Downloads Frameworks, related dSYMs and .version files in the caches.
downloadArtifacts
:: Maybe S3.BucketName -- ^ Just an S3 Bucket name or Nothing
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing
-> Maybe FilePath -- ^ Just the path to the engine or Nothing
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` from which to derive Frameworks, dSYMs and .version files
-> [TargetPlatform] -- ^ A list of `TargetPlatform`s to limit the operation to.
-> ReaderT
( CachePrefix
, SkipLocalCacheFlag
, ConcurrentlyFlag
, Bool
)
RomeMonad
()
downloadArtifacts mS3BucketName mlCacheDir mEnginePath reverseRepositoryMap frameworkVersions platforms
= do
(cachePrefix, skipLocalCacheFlag@(SkipLocalCacheFlag skipLocalCache), concurrentlyFlag@(ConcurrentlyFlag performConcurrently), verbose) <-
ask
let sayFunc :: MonadIO m => String -> m ()
sayFunc = if verbose then sayLnWithTime else sayLn
case (mS3BucketName, mlCacheDir, mEnginePath) of
(Just s3BucketName, lCacheDir, Nothing) -> do
env <- lift getAWSEnv
let uploadDownloadEnv =
(env, cachePrefix, skipLocalCacheFlag, concurrentlyFlag, verbose)
let action1 = runReaderT
(downloadFrameworksAndArtifactsFromCaches s3BucketName
lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
)
uploadDownloadEnv
let action2 = runReaderT
(downloadVersionFilesFromCaches s3BucketName
lCacheDir
gitRepoNamesAndVersions
)
uploadDownloadEnv
if performConcurrently
then liftIO $ concurrently_ action1 action2
else liftIO $ action1 >> action2
(Nothing, Just lCacheDir, Nothing) -> do
let readerEnv = (cachePrefix, verbose)
when skipLocalCache $ throwError conflictingSkipLocalCacheOptionMessage
liftIO $ do
runReaderT
(do
errors <-
mapM runExceptT
$ getAndUnzipFrameworksAndArtifactsFromLocalCache
lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
mapM_ (whenLeft sayFunc) errors
)
readerEnv
runReaderT
(do
errors <- mapM runExceptT $ getAndSaveVersionFilesFromLocalCache
lCacheDir
gitRepoNamesAndVersions
mapM_ (whenLeft sayFunc) errors
)
readerEnv
-- Use engine
(Nothing, lCacheDir, Just ePath) -> do
let engineEnv = (cachePrefix, skipLocalCacheFlag, concurrentlyFlag, verbose)
let action1 = runReaderT
(downloadFrameworksAndArtifactsWithEngine ePath
lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
)
engineEnv
let action2 = runReaderT
(downloadVersionFilesWithEngine ePath
lCacheDir
gitRepoNamesAndVersions
)
engineEnv
if performConcurrently
then liftIO $ concurrently_ action1 action2
else liftIO $ action1 >> action2
-- Misconfigured
(Nothing, Nothing, Nothing) -> throwError allCacheKeysMissingMessage
-- Misconfigured
(Just s3BucketName, _, Just ePath) -> throwError conflictingCachesMessage
where
gitRepoNamesAndVersions :: [ProjectNameAndVersion]
gitRepoNamesAndVersions = repoNamesAndVersionForFrameworkVersions
reverseRepositoryMap
frameworkVersions
-- | Uploads Frameworks and relative dSYMs together with .version files to caches
uploadArtifacts
:: Maybe S3.BucketName -- ^ Just an S3 Bucket name or Nothing
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing
-> Maybe FilePath -- ^ Just the path to the engine or Nothing
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` from which to derive Frameworks, dSYMs and .version files
-> [TargetPlatform] -- ^ A list of `TargetPlatform` to restrict this operation to.
-> ReaderT
( CachePrefix
, SkipLocalCacheFlag
, ConcurrentlyFlag
, Bool
)
RomeMonad
()
uploadArtifacts mS3BucketName mlCacheDir mEnginePath reverseRepositoryMap frameworkVersions platforms
= do
(cachePrefix, skipLocalCacheFlag@(SkipLocalCacheFlag skipLocalCache), concurrentlyFlag@(ConcurrentlyFlag performConcurrently), verbose) <-
ask
case (mS3BucketName, mlCacheDir, mEnginePath) of
-- S3 Cache, but no engine
(Just s3BucketName, lCacheDir, Nothing) -> do
awsEnv <- lift getAWSEnv
let uploadDownloadEnv =
( awsEnv
, cachePrefix
, skipLocalCacheFlag
, concurrentlyFlag
, verbose
)
let action1 = runReaderT
(uploadFrameworksAndArtifactsToCaches s3BucketName
lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
)
uploadDownloadEnv
let action2 = runReaderT
(uploadVersionFilesToCaches s3BucketName
lCacheDir
gitRepoNamesAndVersions
)
uploadDownloadEnv
if performConcurrently
then liftIO $ concurrently_ action1 action2
else liftIO $ action1 >> action2
-- No remotes, just local
(Nothing, Just lCacheDir, Nothing) -> do
let readerEnv = (cachePrefix, verbose)
when skipLocalCache $ throwError conflictingSkipLocalCacheOptionMessage
liftIO
$ runReaderT
(saveFrameworksAndArtifactsToLocalCache lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
)
readerEnv
>> runReaderT
(saveVersionFilesToLocalCache lCacheDir gitRepoNamesAndVersions)
readerEnv
-- Engine, maybe Cache
(Nothing, lCacheDir, Just enginePath) -> do
let engineEnv =
( cachePrefix
, skipLocalCacheFlag
, concurrentlyFlag
, verbose
)
let action1 = runReaderT
(uploadFrameworksAndArtifactsToEngine enginePath
lCacheDir
reverseRepositoryMap
frameworkVersions
platforms
)
engineEnv
let action2 = runReaderT
(uploadVersionFilesToEngine enginePath
lCacheDir
gitRepoNamesAndVersions
)
engineEnv
if performConcurrently
then liftIO $ concurrently_ action1 action2
else liftIO $ action1 >> action2
(Nothing, Nothing, Nothing) -> throwError allCacheKeysMissingMessage
where
gitRepoNamesAndVersions :: [ProjectNameAndVersion]
gitRepoNamesAndVersions = repoNamesAndVersionForFrameworkVersions
reverseRepositoryMap
frameworkVersions
-- | Uploads a lest of .version files to the engine.
uploadVersionFilesToEngine
:: FilePath -- ^ The engine definition.
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing.
-> [ProjectNameAndVersion] -- ^ A list of `ProjectName` and `Version` information.
-> ReaderT (CachePrefix, SkipLocalCacheFlag, ConcurrentlyFlag, Bool) IO ()
uploadVersionFilesToEngine enginePath mlCacheDir =
mapM_ (uploadVersionFileToEngine enginePath mlCacheDir)
-- | Uploads a .version file to the engine.
uploadVersionFileToEngine
:: FilePath -- ^ The engine definition.
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing.
-> ProjectNameAndVersion -- ^ The information used to derive the name and path for the .version file.
-> ReaderT (CachePrefix, SkipLocalCacheFlag, ConcurrentlyFlag, Bool) IO ()
uploadVersionFileToEngine enginePath mlCacheDir projectNameAndVersion = do
(cachePrefix, SkipLocalCacheFlag skipLocalCache, _, verbose) <- ask
versionFileExists <- liftIO $ doesFileExist versionFileLocalPath
when versionFileExists $ do
versionFileContent <- liftIO $ LBS.readFile versionFileLocalPath
unless skipLocalCache
$ maybe (return ()) liftIO
$ saveVersionFileBinaryToLocalCache
<$> mlCacheDir
<*> Just cachePrefix
<*> Just versionFileContent
<*> Just projectNameAndVersion
<*> Just verbose
liftIO $ runReaderT
(uploadVersionFileToEngine' enginePath
versionFileContent
projectNameAndVersion
)
(cachePrefix, verbose)
where
versionFileName = versionFileNameForProjectName $ fst projectNameAndVersion
versionFileLocalPath = carthageBuildDirectory </> versionFileName
-- | Uploads a lest of .version files to the caches.
uploadVersionFilesToCaches
:: S3.BucketName -- ^ The cache definition.
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing.
-> [ProjectNameAndVersion] -- ^ A list of `ProjectName` and `Version` information.
-> ReaderT UploadDownloadCmdEnv IO ()
uploadVersionFilesToCaches s3Bucket mlCacheDir =
mapM_ (uploadVersionFileToCaches s3Bucket mlCacheDir)
-- | Uploads a .version file the caches.
uploadVersionFileToCaches
:: S3.BucketName -- ^ The cache definition.
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing.
-> ProjectNameAndVersion -- ^ The information used to derive the name and path for the .version file.
-> ReaderT UploadDownloadCmdEnv IO ()
uploadVersionFileToCaches s3BucketName mlCacheDir projectNameAndVersion = do
(env, cachePrefix, SkipLocalCacheFlag skipLocalCache, _, verbose) <- ask
versionFileExists <- liftIO $ doesFileExist versionFileLocalPath
when versionFileExists $ do
versionFileContent <- liftIO $ LBS.readFile versionFileLocalPath
unless skipLocalCache
$ maybe (return ()) liftIO
$ saveVersionFileBinaryToLocalCache
<$> mlCacheDir
<*> Just cachePrefix
<*> Just versionFileContent
<*> Just projectNameAndVersion
<*> Just verbose
liftIO $ runReaderT
(uploadVersionFileToS3 s3BucketName
versionFileContent
projectNameAndVersion
)
(env, cachePrefix, verbose)
where
versionFileName = versionFileNameForProjectName $ fst projectNameAndVersion
versionFileLocalPath = carthageBuildDirectory </> versionFileName
-- | Uploads a list of Frameworks and relative dSYMs to the caches.
uploadFrameworksAndArtifactsToCaches
:: S3.BucketName -- ^ The cache definition.
-> Maybe FilePath -- ^ Just the path to a local cache or Nothing
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` identifying the Frameworks and dSYMs.
-> [TargetPlatform] -- ^ A list of `TargetPlatform`s restricting the scope of this action.
-> ReaderT UploadDownloadCmdEnv IO ()
uploadFrameworksAndArtifactsToCaches s3BucketName mlCacheDir reverseRomeMap fvs platforms
= do
(_, _, _, ConcurrentlyFlag performConcurrently, _) <- ask
if performConcurrently
then mapConcurrently_ uploadConcurrently fvs
else mapM_ (sequence . upload) platforms
where
uploadConcurrently f = mapConcurrently
(uploadFrameworkAndArtifactsToCaches s3BucketName
mlCacheDir
reverseRomeMap
f
)
platforms
upload = mapM
(uploadFrameworkAndArtifactsToCaches s3BucketName mlCacheDir reverseRomeMap)
fvs
-- | Uploads a Framework, the relative dSYM and bcsymbolmaps to the caches.
uploadFrameworkAndArtifactsToCaches
:: S3.BucketName -- ^ The cache definition.
-> Maybe FilePath -- ^ Just the path to the local cache or Nothing.
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> FrameworkVersion -- ^ The `FrameworkVersion` identifying the Framework and the dSYM
-> TargetPlatform -- ^ A `TargetPlatform` restricting the scope of this action.
-> ReaderT UploadDownloadCmdEnv IO ()
uploadFrameworkAndArtifactsToCaches s3BucketName mlCacheDir reverseRomeMap fVersion@(FrameworkVersion f@(Framework fwn _ _) _) platform
= do
(env, cachePrefix, s@(SkipLocalCacheFlag skipLocalCache), _, verbose) <- ask
let uploadDownloadEnv = (env, cachePrefix, verbose)
void . runExceptT $ do
frameworkArchive <- createZipArchive frameworkDirectory verbose
unless skipLocalCache
$ maybe (return ()) liftIO
$ runReaderT
<$> ( saveFrameworkToLocalCache
<$> mlCacheDir
<*> Just frameworkArchive
<*> Just reverseRomeMap
<*> Just fVersion
<*> Just platform
)
<*> Just (cachePrefix, s, verbose)
liftIO $ runReaderT
(uploadFrameworkToS3 frameworkArchive
s3BucketName
reverseRomeMap
fVersion
platform
)
uploadDownloadEnv
void . runExceptT $ do
dSYMArchive <- createZipArchive dSYMdirectory verbose
unless skipLocalCache
$ maybe (return ()) liftIO
$ runReaderT
<$> ( saveDsymToLocalCache
<$> mlCacheDir
<*> Just dSYMArchive
<*> Just reverseRomeMap
<*> Just fVersion
<*> Just platform
)
<*> Just (cachePrefix, s, verbose)
liftIO $ runReaderT
(uploadDsymToS3 dSYMArchive
s3BucketName
reverseRomeMap
fVersion
platform
)
uploadDownloadEnv
void . runExceptT $ do
dwarfUUIDs <- dwarfUUIDsFrom (frameworkDirectory </> fwn)
maybeUUIDsArchives <- liftIO $ forM dwarfUUIDs $ \dwarfUUID ->
runMaybeT $ do
dwarfArchive <- exceptToMaybeT
$ createZipArchive (bcSymbolMapPath dwarfUUID) verbose
return (dwarfUUID, dwarfArchive)
unless skipLocalCache
$ forM_ maybeUUIDsArchives
$ mapM
$ \(dwarfUUID, dwarfArchive) ->
maybe (return ()) liftIO
$ runReaderT
<$> ( saveBcsymbolmapToLocalCache
<$> mlCacheDir
<*> Just dwarfUUID
<*> Just dwarfArchive
<*> Just reverseRomeMap
<*> Just fVersion
<*> Just platform
)
<*> Just (cachePrefix, s, verbose)
forM_ maybeUUIDsArchives $ mapM $ \(dwarfUUID, dwarfArchive) ->
liftIO $ runReaderT
(uploadBcsymbolmapToS3 dwarfUUID
dwarfArchive
s3BucketName
reverseRomeMap
fVersion
platform
)
uploadDownloadEnv
where
frameworkNameWithFrameworkExtension = appendFrameworkExtensionTo f
platformBuildDirectory =
carthageArtifactsBuildDirectoryForPlatform platform f
frameworkDirectory =
platformBuildDirectory </> frameworkNameWithFrameworkExtension
dSYMNameWithDSYMExtension = frameworkNameWithFrameworkExtension <> ".dSYM"
dSYMdirectory = platformBuildDirectory </> dSYMNameWithDSYMExtension
bcSymbolMapPath d = platformBuildDirectory </> bcsymbolmapNameFrom d
-- | Saves a list of Frameworks, relative dSYMs and bcsymbolmaps to a local cache.
saveFrameworksAndArtifactsToLocalCache
:: MonadIO m
=> FilePath -- ^ The cache definition.
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> [FrameworkVersion] -- ^ A list of `FrameworkVersion` identifying Frameworks and dSYMs
-> [TargetPlatform] -- ^ A list of `TargetPlatform` restricting the scope of this action.
-> ReaderT (CachePrefix, Bool) m ()
saveFrameworksAndArtifactsToLocalCache lCacheDir reverseRomeMap fvs = mapM_
(sequence . save)
where
save =
mapM (saveFrameworkAndArtifactsToLocalCache lCacheDir reverseRomeMap) fvs
-- | Saves a Framework, the relative dSYM and then bcsymbolmaps to a local cache.
saveFrameworkAndArtifactsToLocalCache
:: MonadIO m
=> FilePath -- ^ The cache definition
-> InvertedRepositoryMap -- ^ The map used to resolve `FrameworkName`s to `ProjectName`s.
-> FrameworkVersion -- ^ A `FrameworkVersion` identifying Framework and dSYM.
-> TargetPlatform -- ^ A `TargetPlatform` restricting the scope of this action.
-> ReaderT (CachePrefix, Bool) m ()
saveFrameworkAndArtifactsToLocalCache lCacheDir reverseRomeMap fVersion@(FrameworkVersion f@(Framework fwn _ _) _) platform
= do
(cachePrefix, verbose) <- ask
let readerEnv = (cachePrefix, SkipLocalCacheFlag False, verbose)