Skip to content

Commit

Permalink
Handle spaces in file names, fixes #438 (#439)
Browse files Browse the repository at this point in the history
* Handle everything in filenames, hopefully

* Use non-mangled file name when writing test file
  • Loading branch information
eldritchconundrum authored Aug 10, 2024
1 parent 0879fd7 commit dd4b6af
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 6 deletions.
17 changes: 15 additions & 2 deletions Checker/main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ let runCommand argv =
minifier.Format(out, options)
out.ToString() |> cleanString
let outdir = "tests/out/" + Regex.Replace(options.outputName, @"^tests/(.*)/[^/]*$", @"$1") + "/"
let split = Regex.Match(shader.mangledFilename, @"(^.*)_([^_]+)$").Groups
let split = Regex.Match(System.IO.Path.GetFileName shader.filename, @"(^.*)\.([^\.]+)$").Groups
let name = split[1].Value
let ext = split[2].Value
Directory.CreateDirectory outdir |> ignore
Expand All @@ -219,12 +219,25 @@ let runCommand argv =
1

let testGolden () =
let splitArgs line =
let mutable args = []
let mutable insideQuotes = false
let mutable arg = ""
for c in line do
match c with
| '"' -> insideQuotes <- not insideQuotes
| ' ' when not insideQuotes ->
args <- arg :: args
arg <- ""
| c -> arg <- $"{arg}{c}"
if arg <> "" then args <- arg :: args
args |> List.rev
let commands = File.ReadAllLines "tests/commands.txt" |> Array.choose (fun line ->
let line = line.Trim()
if line.Length = 0 || line.[0] = '#' then
None
else
Some (line.Split([|' '|]))
Some (splitArgs line |> List.toArray)
)
commands |> Array.Parallel.map runCommand |> Array.sum

Expand Down
7 changes: 6 additions & 1 deletion src/ast.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Ast

open System.Text.RegularExpressions

[<RequireQualifiedAccess>]
type VarScope = Global | Local | Parameter

Expand Down Expand Up @@ -188,12 +190,15 @@ type ExportedName = {
newName: string
}

let mangleToAscii s = Regex.Replace(s, @"[^a-zA-Z_0-9]", "_") // é -> _
let mangleToUnicode s = Regex.Replace(s, @"\W", "_") // é -> é

type Shader = {
filename: string
code: TopLevel list
forbiddenNames: string list
reorderFunctions: bool // set to true if we saw a forward declaration
} with member this.mangledFilename = (System.IO.Path.GetFileName this.filename).Replace(".", "_")
} with member this.mangledFilename = mangleToUnicode (System.IO.Path.GetFileName this.filename)

// mapEnv is a kind of visitor that applies transformations to statements and expressions,
// while also collecting visible variable and function declarations along the way.
Expand Down
7 changes: 4 additions & 3 deletions src/formatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

open System
open System.IO
open System.Text.RegularExpressions

type private Impl(options: Options.Options, withLocations) =

Expand Down Expand Up @@ -42,7 +43,7 @@ type private Impl(options: Options.Options, withLocations) =
let fileName =
if options.outputName = "" || options.outputName = "-" then "shader_code.h"
else Path.GetFileName options.outputName
let macroName = fileName.Replace(".", "_").ToUpper() + "_"
let macroName = Ast.mangleToAscii(System.IO.Path.GetFileName fileName).ToUpper() + "_"

fprintfn out "// Generated with Shader Minifier %s (https://github.com/laurentlb/Shader_Minifier/)" Options.version

Expand All @@ -54,7 +55,7 @@ type private Impl(options: Options.Options, withLocations) =

fprintfn out ""
for shader in shaders do
fprintfn out "const char *%s =" shader.mangledFilename
fprintfn out "const char *%s =" (Ast.mangleToAscii shader.mangledFilename)
let lines = String.concat Environment.NewLine [
for indent, line in getLines shader do
sprintf " %s\"%s\"" indent (escape line)]
Expand Down Expand Up @@ -127,7 +128,7 @@ type private Impl(options: Options.Options, withLocations) =
for shader in shaders do
// fprintfn out "_%s:%s\tdb '%s', 0" shader.mangledFilename Environment.NewLine (minify shader)

fprintfn out "_%s:" shader.mangledFilename // \tdb '%s', 0" shader.mangledFilename Environment.NewLine (minify shader)
fprintfn out "_%s:" (Ast.mangleToAscii shader.mangledFilename) // \tdb '%s', 0" shader.mangledFilename Environment.NewLine (minify shader)
let lines = String.concat Environment.NewLine [
for indent, line in getLines shader do
sprintf "\tdb %s'%s'" indent (escape line)]
Expand Down
4 changes: 4 additions & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
--format js -o tests/real/disco.expected tests/real/disco.frag
--format nasm -o tests/real/leizex.expected tests/real/leizex.frag
--format rust -o tests/real/heart.expected tests/real/heart.frag
--format c-variables -o "tests/unit/file name with-weird caractères.frag.c-variables.expected" "tests/unit/file name with-weird caractères.frag"
--format js -o "tests/unit/file name with-weird caractères.frag.js.expected" "tests/unit/file name with-weird caractères.frag"
--format nasm -o "tests/unit/file name with-weird caractères.frag.nasm.expected" "tests/unit/file name with-weird caractères.frag"
--format rust -o "tests/unit/file name with-weird caractères.frag.rust.expected" "tests/unit/file name with-weird caractères.frag"

# Unit tests

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/file name with-weird caractères.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// maybe the real test data was the file name we made along the way
void main() { gl_FragColor = vec4(0.33, 0.66, 0.99, 1.); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated with (https://github.com/laurentlb/Shader_Minifier/)
#ifndef FILE_NAME_WITH_WEIRD_CARACT_RES_FRAG_C_VARIABLES_EXPECTED_
# define FILE_NAME_WITH_WEIRD_CARACT_RES_FRAG_C_VARIABLES_EXPECTED_

const char *file_name_with_weird_caract_res_frag =
"void main()"
"{"
"gl_FragColor=vec4(.33,.66,.99,1);"
"}";

#endif // FILE_NAME_WITH_WEIRD_CARACT_RES_FRAG_C_VARIABLES_EXPECTED_
3 changes: 3 additions & 0 deletions tests/unit/file name with-weird caractères.frag.js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generated with (https://github.com/laurentlb/Shader_Minifier/)

var file_name_with_weird_caractères_frag = `void main(){gl_FragColor=vec4(.33,.66,.99,1);}`
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Generated with (https://github.com/laurentlb/Shader_Minifier/)

_file_name_with_weird_caract_res_frag:
db 'void main()'
db '{'
db 'gl_FragColor=vec4(.33,.66,.99,1);'
db '}', 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated with (https://github.com/laurentlb/Shader_Minifier/)

pub const FILE_NAME_WITH_WEIRD_CARACTÈRES_FRAG: &'static [u8] = b"\
void main()\
{\
gl_FragColor=vec4(.33,.66,.99,1);\
}\0";

0 comments on commit dd4b6af

Please sign in to comment.