Skip to content

Commit

Permalink
Dev container (fsprojects#2980)
Browse files Browse the repository at this point in the history
* Update fsdocs to v20 alpha

* Add devcontainer config

* Update getting started

* Build devcontainer

* cmd as one line?

* -p Init is ran as postCreateCommand cmd.

* Use docker file

* Add some handy bash functions

* Use lf in config file test

* Use explicit lf in unit test.
  • Loading branch information
nojaf committed Nov 13, 2023
1 parent b2d0215 commit 77f6444
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"fsdocs-tool": {
"version": "19.1.1",
"version": "20.0.0-alpha-009",
"commands": [
"fsdocs"
]
Expand Down
18 changes: 18 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0.100-rc.2

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# Configure apt and install packages
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
# Verify bash, git, process tools, lsb-release (common in install instructions for CLIs) installed
&& apt-get -y install bash git openssh-client less iproute2 procps lsb-release \
&& apt-get -y install gnupg \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog
36 changes: 36 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/dotnet-fsharp
{
"name": "Fantomas",
"build": {
"dockerfile": "Dockerfile"
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"Ionide.Ionide-fsharp",
"ms-dotnettools.csharp",
"tintoy.msbuild-project-tools"
]
}
},
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [ 8901 ],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "dotnet restore",

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
}
51 changes: 51 additions & 0 deletions .devcontainer/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

alias ni="touch"
# alias dtr="dotnet tool restore"
function dtr () {
# Extract lines that contain tool names and versions
tools=$(grep -oP '"\K[^"]+(?=": {)' .config/dotnet-tools.json | grep -v '^tools$')
versions=$(grep -oP 'version": "\K[^"]+' .config/dotnet-tools.json)

# Convert strings to arrays
readarray -t tools_array <<<"$tools"
readarray -t versions_array <<<"$versions"

# Loop through the arrays
for i in "${!tools_array[@]}"; do
tool_name=${tools_array[$i]}
tool_version=${versions_array[$i]}

# Install the tool
dotnet tool install "$tool_name" --version "$tool_version"
done
}

function format-changed() {
# Get the root of the current git repository
repo_root=$(git rev-parse --show-toplevel)

# Run git status --porcelain in the root directory and save the output as an array
mapfile -t status_array < <(git -C "$repo_root" status --porcelain)

# Filter the file names in the status_array array
filtered_files=()
for file in "${status_array[@]}"; do
if [[ $file =~ \.fs$ || $file =~ \.fsi$ || $file =~ \.fsx$ ]]; then
# Trim leading symbols before the first space
trimmed_file="${file##* }"
filtered_files+=("$trimmed_file")
fi
done

# Concatenate the filtered file names into a single string with spaces
concatenated_files="${filtered_files[*]}"

pushd "$repo_root" > /dev/null || exit 1

# Run fantomas
echo "dotnet fantomas $concatenated_files"
dotnet fantomas $concatenated_files

popd > /dev/null || exit
}
6 changes: 0 additions & 6 deletions .githooks/pre-commit

This file was deleted.

10 changes: 10 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ jobs:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: dotnet fsi build.fsx -p Release

# Builds the project in a dev container
build-devcontainer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build and run dev container task
uses: devcontainers/ci@v0.3
with:
runCmd: dotnet fsi build.fsx
38 changes: 35 additions & 3 deletions build.fsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#r "nuget: Fun.Build, 0.5.3"
#r "nuget: Fun.Build, 1.0.3"
#r "nuget: CliWrap, 3.6.4"
#r "nuget: FSharp.Data, 6.3.0"
#r "nuget: Ionide.KeepAChangelog, 0.1.8"
#r "nuget: Humanizer.Core, 2.14.1"

open System
open System.Text.Json
open System.IO
open Fun.Build
open CliWrap
Expand All @@ -27,6 +28,34 @@ let cleanFolders (input: string seq) =
Directory.Delete(dir, true))
}

/// Workaround for https://github.com/dotnet/sdk/issues/35989
let restoreTools (ctx: Internal.StageContext) =
async {
let json = File.ReadAllText ".config/dotnet-tools.json"
let jsonDocument = JsonDocument.Parse(json)
let root = jsonDocument.RootElement
let tools = root.GetProperty("tools")

let! installs =
tools.EnumerateObject()
|> Seq.map (fun tool ->
let version = tool.Value.GetProperty("version").GetString()
ctx.RunCommand $"dotnet tool install %s{tool.Name} --version %s{version}")
|> Async.Sequential

let failedInstalls =
installs
|> Array.tryPick (function
| Ok _ -> None
| Error error -> Some error)

match failedInstalls with
| None -> return 0
| Some error ->
printfn $"%s{error}"
return 1
}

let benchmarkAssembly =
"src"
</> "Fantomas.Benchmarks"
Expand Down Expand Up @@ -61,7 +90,7 @@ let analysisReportsDir = "analysisreports"

pipeline "Build" {
workingDir __SOURCE_DIRECTORY__
stage "RestoreTools" { run "dotnet tool restore" }
stage "RestoreTools" { run restoreTools }
stage "Clean" {
run (
cleanFolders
Expand Down Expand Up @@ -172,11 +201,14 @@ pipeline "PushClient" {
pipeline "Docs" {
workingDir __SOURCE_DIRECTORY__
stage "Prepare" {
run "dotnet tool restore"
run restoreTools
run "dotnet build -c Release src/Fantomas/Fantomas.fsproj"
}
stage "Watch" {
paralle
envVars
[| "DOTNET_ROLL_FORWARD_TO_PRERELEASE", "1"
"DOTNET_ROLL_FORWARD", "LatestMajor" |]
run "dotnet fsi ./docs/.style/style.fsx --watch"
run
$"dotnet fsdocs watch --properties Configuration=Release --fscoptions \" -r:{semanticVersioning}\" --eval --nonpublic"
Expand Down
74 changes: 74 additions & 0 deletions docs/.style/fsdocs-custom.sass
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,77 @@ body

.copy-icon
cursor: pointer

// Search
#search-btn
cursor: pointer
&:hover
color: $primary

::backdrop
background-color: $black
opacity: 0.5

dialog
background-color: $pearl
margin: 4rem auto
padding: 0.25rem
border: none
opacity: 0
visibility: hidden
transition: all 0.2s ease-in-out
border-radius: 6px
width: 500px

&[open]
opacity: 1
visibility: visible


& input[type=search]
margin: 0
display: block
width: 100%
padding: 0.75rem
outline: 1px solid $primary
border-radius: 6px
border: none

&::placeholder
font-size: 1.25rem

.empty
color: $gray-500
text-align: center
font-size: 0.80rem
margin: 0.75rem


& ul
overflow-y: auto
max-height: 50vh
list-style: none
padding: 0

& li
margin: 0
border-bottom: 1px solid $gray-200

&:hover
& a, & a iconify-icon
background-color: $secondary
color: $white

& a
display: flex
padding: 0.75rem
overflow-x: hidden

& iconify-icon
color: $black
width: 24px
margin-right: 0.5rem




2 changes: 1 addition & 1 deletion docs/_menu-item_template.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<li class="nav-item"><a href="{{fsdocs-menu-item-link}}" class="nav-link pb-2 d-block" > {{fsdocs-menu-item-content}}</a></li>
<li class="nav-item {{fsdocs-menu-item-active-class}}"><a href="{{fsdocs-menu-item-link}}" class="nav-link pb-2 d-block" > {{fsdocs-menu-item-content}}</a></li>
2 changes: 1 addition & 1 deletion docs/_menu_template.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="{{fsdocs-menu-header-id}}-wrapper" class="accordion-item">
<div id="{{fsdocs-menu-header-id}}-wrapper" class="accordion-item {{fsdocs-menu-header-active-class}}">
<li class="docs-menu">
<button id="{{fsdocs-menu-header-id}}-button"
class="accordion-button collapsed btn text-white d-flex justify-content-between align-items-center"
Expand Down
42 changes: 11 additions & 31 deletions docs/_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
<link rel="shortcut icon" type="image/x-icon" href="{{root}}images/favicon.ico">
<link type="text/css" rel="stylesheet" href="{{root}}content/fsdocs-custom.css" />
<script type="text/javascript" src="{{root}}content/fsdocs-tips.js"></script>
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
<!-- END SEARCH BOX: this adds support for the search box -->
<script src="https://code.iconify.design/iconify-icon/1.0.7/iconify-icon.min.js"></script>
{{fsdocs-watch-script}}
</head>

Expand Down Expand Up @@ -53,23 +50,7 @@
<a class="nav-link active" aria-current="page" href="{{root}}docs/index.html">Docs</a>
<a class="nav-link" target="_blank" href="https://fsprojects.github.io/fantomas-tools/">Tools</a>
</div>
<form class="d-flex">
<div class="input-group me-4">
<label for="search-by">
<i class="fas fa-search"></i>
</label>
<div class="btn border-end-0 border">
<i class="bi bi-search"></i>
</div>
<input aria-label="Search" class="form-control border-start-0 border" data-search-input=""
id="search-by" type="search" placeholder="Search..." />
<span class="input-group-append">
</span>
<span data-search-clear="">
<i class="fas fa-times"></i>
</span>
</div>
</form>
<i class="bi bi-search text-black me-2" id="search-btn"></i>
<a class="nav-link m-0 p-0" target="_blank" href="{{fsdocs-repository-link}}">GitHub<i
class="bi bi-box-arrow-up-right mx-2"></i></a>
</div>
Expand All @@ -81,16 +62,13 @@
</div>
<div id="push"></div>
</main>
<script type="module" src="{{root}}content/docs-setup.js"></script>
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
<script type="text/javascript">var fsdocs_search_baseurl = '{{root}}';</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js"></script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.min.js"></script>
<script type="text/javascript" src="{{root}}content/fsdocs-search.js"></script>
<!-- END SEARCH BOX: this adds support for the search box -->
<dialog>
<input type="search" placeholder="Search docs" />
<div class="results">
<ul></ul>
<p class="empty">Type something to start searching.</p>
</div>
</dialog>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<script type="module" src="{{root}}content/webcomponents.js"></script>
<script type="module">
Expand All @@ -103,5 +81,7 @@
}
});
</script>
<script type="module" src="{{root}}content/docs-setup.js"></script>
<script type="module" src="{{root}}content/fsdocs-search.js"></script>
</body>
</html>
Loading

0 comments on commit 77f6444

Please sign in to comment.