Skip to content

Commit 5f34d78

Browse files
committed
custom Setup.hs to generate Functora.Miso.Theme using from css files
1 parent adab442 commit 5f34d78

File tree

4 files changed

+331
-2
lines changed

4 files changed

+331
-2
lines changed

ghcjs/miso-functora/Setup.hs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE DerivingStrategies #-}
4+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+
{-# LANGUAGE LambdaCase #-}
6+
{-# LANGUAGE OverloadedStrings #-}
7+
{-# LANGUAGE PackageImports #-}
8+
{-# LANGUAGE TypeApplications #-}
9+
{-# LANGUAGE NoImplicitPrelude #-}
10+
{-# OPTIONS_GHC -Werror #-}
11+
{-# OPTIONS_GHC -Weverything #-}
12+
{-# OPTIONS_GHC -Wno-all-missed-specialisations #-}
13+
{-# OPTIONS_GHC -Wno-missed-specialisations #-}
14+
{-# OPTIONS_GHC -Wno-missing-exported-signatures #-}
15+
{-# OPTIONS_GHC -Wno-missing-import-lists #-}
16+
{-# OPTIONS_GHC -Wno-missing-local-signatures #-}
17+
{-# OPTIONS_GHC -Wno-safe #-}
18+
{-# OPTIONS_GHC -Wno-unsafe #-}
19+
{-# OPTIONS_GHC -fprint-potential-instances #-}
20+
21+
import qualified Data.Text as T
22+
import Distribution.Simple hiding (Module (..))
23+
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo (..))
24+
import Distribution.Simple.Utils (writeUTF8File)
25+
import GHC (runGhc)
26+
import GHC.Paths (libdir)
27+
import GHC.SourceGen
28+
import qualified System.Directory as Directory
29+
import System.Environment (getProgName)
30+
import qualified System.FilePath as FilePath
31+
import qualified Text.Casing as Casing
32+
import Universum hiding (empty)
33+
34+
#if MIN_VERSION_ghc(9,0,0)
35+
import "ghc" GHC.Driver.Session (getDynFlags)
36+
import "ghc" GHC.Utils.Outputable
37+
( Outputable (..),
38+
text,
39+
vcat,
40+
($+$),
41+
)
42+
#else
43+
import "ghc" DynFlags (getDynFlags)
44+
import "ghc" Outputable
45+
( Outputable (..),
46+
text,
47+
vcat,
48+
($+$),
49+
)
50+
#endif
51+
52+
main :: IO ()
53+
main =
54+
defaultMainWithHooks
55+
simpleUserHooks
56+
{ buildHook = \p l h f ->
57+
codeGenHook l
58+
>> buildHook simpleUserHooks p l h f
59+
}
60+
61+
data Module = Module
62+
{ moduleHeader :: [String],
63+
moduleBody :: HsModule'
64+
}
65+
66+
instance Outputable Module where
67+
ppr m =
68+
vcat (text <$> moduleHeader m)
69+
$+$ ppr (moduleBody m)
70+
71+
codeGenHook :: LocalBuildInfo -> IO ()
72+
codeGenHook _ = do
73+
prog <- getProgName
74+
runGhc (Just libdir) $ do
75+
dflags <-
76+
getDynFlags
77+
cssKebab <-
78+
liftIO $ FilePath.takeBaseName <<$>> Directory.listDirectory "css/"
79+
let cssPascal =
80+
fmap Casing.pascal cssKebab
81+
when (cssKebab /= fmap Casing.kebab cssPascal)
82+
. error
83+
$ "Bad kebab <-> pascal isomorphism in "
84+
<> show cssKebab
85+
liftIO
86+
. writeFileIfChanged
87+
. showPpr dflags
88+
$ generateCode prog cssPascal
89+
90+
generateCode :: String -> [String] -> Module
91+
generateCode prog css =
92+
Module
93+
{ moduleHeader = header,
94+
moduleBody =
95+
module'
96+
(Just "Functora.Miso.Theme")
97+
(Just [thingAll "Theme"])
98+
( fmap (qualified' . import')
99+
$ [ "Prelude",
100+
"Data.Data",
101+
"GHC.Generics"
102+
]
103+
)
104+
[ data'
105+
"Theme"
106+
mempty
107+
( fmap (flip prefixCon mempty . fromString) css
108+
)
109+
[ derivingStock
110+
$ fmap
111+
var
112+
[ "Eq",
113+
"Ord",
114+
"Show",
115+
"Read",
116+
"Data",
117+
"Generic",
118+
"Enum",
119+
"Bounded"
120+
]
121+
]
122+
]
123+
}
124+
where
125+
header =
126+
[ "{- DO NOT EDIT. This file was auto-generated by the "
127+
<> prog
128+
<> " program. -}"
129+
-- languagePragma "DeriveDataTypeable",
130+
-- optionsGhcPragma "-Wno-missing-export-lists"
131+
]
132+
133+
-- languagePragma, optionsGhcPragma :: String -> String
134+
-- languagePragma s = "{-# LANGUAGE " <> s <> "#-}"
135+
-- optionsGhcPragma s = "{-# OPTIONS_GHC " <> s <> "#-}"
136+
137+
writeFileIfChanged :: String -> IO ()
138+
writeFileIfChanged newFile = do
139+
oldFile <-
140+
(Just . T.unpack <$> readFile doNotEditFilePath)
141+
`catchAny` const (pure Nothing)
142+
when (oldFile /= Just newFile)
143+
$ writeUTF8File doNotEditFilePath newFile
144+
145+
doNotEditFilePath :: FilePath
146+
doNotEditFilePath =
147+
"src/Functora/Miso/Theme.hs"

ghcjs/miso-functora/miso-functora.cabal

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ name: miso-functora
33
version: 0.1.0.0
44
synopsis: Miso Widgets
55
category: Web
6-
build-type: Simple
6+
build-type: Custom
77

88
flag ghcid
99
manual: True
1010
default: False
1111

12+
custom-setup
13+
setup-depends:
14+
, base
15+
, Cabal
16+
, casing
17+
, directory
18+
, filepath
19+
, ghc
20+
, ghc-paths
21+
, ghc-source-gen ==0.4.2.0
22+
, text
23+
, universum
24+
1225
common pkg
1326
default-language: Haskell2010
1427
other-modules: Paths_miso_functora
@@ -88,6 +101,11 @@ common pkg
88101
build-depends: ghcjs-base
89102
js-sources: js/main.min.js
90103

104+
data-files:
105+
css/*.css
106+
dist/**/*.css
107+
dist/**/*.ttf
108+
91109
library
92110
import: pkg
93111
hs-source-dirs: src
@@ -141,6 +159,7 @@ test-suite miso-functora-test
141159
Functora.Miso.Jsm.Specific
142160
Functora.Miso.Orphan
143161
Functora.Miso.Prelude
162+
Functora.Miso.Theme
144163
Functora.Miso.Types
145164
Functora.Miso.Widgets.BrowserLink
146165
Functora.Miso.Widgets.Currency
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{- DO NOT EDIT. This file was auto-generated by the setup program. -}
2+
module Functora.Miso.Theme (
3+
Theme(..)
4+
) where
5+
import qualified Prelude
6+
import qualified Data.Data
7+
import qualified GHC.Generics
8+
data Theme
9+
= MdRetro |
10+
Simple |
11+
Propeller |
12+
Motherplate |
13+
Hack |
14+
Bullframe |
15+
Scooter |
16+
AttriMidnightGreen |
17+
AttriDarkFairyPink |
18+
AwsmPearllusta |
19+
Sakura |
20+
New |
21+
Codify |
22+
Marx |
23+
Picnic |
24+
Centigram |
25+
SkeletonFramework |
26+
Brutalist |
27+
W3cOldstyle |
28+
Wing |
29+
W3cTraditional |
30+
Milligram |
31+
AwsmBigstone |
32+
Koochak |
33+
Tui |
34+
Holiday |
35+
AwsmTasman |
36+
Yorha |
37+
Tent |
38+
Caramel |
39+
W3cUltramarine |
40+
BootCyborg |
41+
Neat |
42+
Gutenberg |
43+
Almond |
44+
BootSuperhero |
45+
Yamb |
46+
AwsmPastelpink |
47+
Clmaterial |
48+
Style |
49+
Water |
50+
Cutestrap |
51+
AwsmBlack |
52+
Ok |
53+
AdsNotebook |
54+
MdModest |
55+
AdsMedium |
56+
MinimalStylesheet |
57+
BootSpacelab |
58+
BootFlatly |
59+
MdAir |
60+
Bahunya |
61+
Latex |
62+
Materialize |
63+
Axist |
64+
SkeletonPlus |
65+
MdSplendor |
66+
AwsmMischka |
67+
MissingStyle |
68+
Furtive |
69+
Shoelace |
70+
BootSlate |
71+
W3cSwiss |
72+
Caiuss |
73+
AttriBrightLightGreen |
74+
Spectre |
75+
Bamboo |
76+
Superstylin |
77+
Pavilion |
78+
W3cModernist |
79+
Terminal |
80+
Gd |
81+
GithubMarkdown |
82+
W3cMidnight |
83+
NoClass |
84+
Concrete |
85+
AwsmGondola |
86+
Roble |
87+
Pico |
88+
Min |
89+
AwsmWhite |
90+
Vanilla |
91+
Lissom |
92+
Material |
93+
Cirrus |
94+
AttriLightFairyPink |
95+
AdsGazette |
96+
Primer |
97+
Mobi |
98+
Skeleton |
99+
Vital |
100+
Hello |
101+
Writ |
102+
Hyp |
103+
Pure |
104+
A11yana |
105+
HtmlStarterkit |
106+
Markdown |
107+
BootPaper |
108+
Minimal |
109+
Basic |
110+
BootReadable |
111+
FlatUi |
112+
Sanitize |
113+
Lemon |
114+
Paper |
115+
Concise |
116+
Chota |
117+
Spcss |
118+
Mini |
119+
Centurion |
120+
AttriDarkForestGreen |
121+
BootCerulean |
122+
AdsTufte |
123+
Bolt |
124+
Preface |
125+
Mercury |
126+
W3cSteely |
127+
Papier |
128+
OhMyCss |
129+
Magick |
130+
Kraken |
131+
Bonsai |
132+
Kathamo |
133+
AwsmDefault |
134+
Classless |
135+
Base |
136+
PandocScholar |
137+
Bare |
138+
SemanticUi |
139+
Thao |
140+
Normalize |
141+
Lotus |
142+
Siimple |
143+
BootDarkly |
144+
BootJournal |
145+
Mu |
146+
Fluidity |
147+
Generic |
148+
Lit |
149+
Snack |
150+
BootCosmo |
151+
BootLumen |
152+
Hiq |
153+
Stylize |
154+
Mui |
155+
Mvp |
156+
Kube |
157+
BootSandstone |
158+
W3cChocolate |
159+
Tacit |
160+
Cardinal |
161+
BootYeti |
162+
Comet
163+
deriving stock (Eq, Ord, Show, Read, Data, Generic, Enum, Bounded)

ghcjs/miso-functora/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const css = {
3939
},
4040
mode: "production",
4141
optimization: {
42-
minimize: false,
42+
minimize: true,
4343
minimizer: [new CssMinimizerPlugin()],
4444
},
4545
plugins: [

0 commit comments

Comments
 (0)