Skip to content

Instantly share code, notes, and snippets.

View bogdanbujdea's full-sized avatar
💭
Who reads this?

Bogdan Bujdea bogdanbujdea

💭
Who reads this?
View GitHub Profile
@bogdanbujdea
bogdanbujdea / UsingsAnalyzer.cs
Created April 27, 2018 07:53
RegisterSyntaxTreeAction
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxTreeAction(AnalyzeTree);
}
@bogdanbujdea
bogdanbujdea / UsingsAnalyzer.cs
Last active April 27, 2018 08:31
Register, grab usings, sort them, create diagnostic reports
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxTreeAction(AnalyzeTree);
}
private void AnalyzeTree(SyntaxTreeAnalysisContext obj)
{
var fileName = obj.Tree.FilePath.Split('\\').Last();
var syntaxNode = obj.Tree.GetRoot();
var descendantNodes = syntaxNode.DescendantNodes();
@bogdanbujdea
bogdanbujdea / RegisterCodeFixesAsync.cs
Created April 27, 2018 08:34
RegisterCodeFixesAsync.cs
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics.Where(d => FixableDiagnosticIds.Contains(d.Id)))
{
context.RegisterCodeFix(CodeAction.Create("Arrange using statements",
token => GetTransformedDocumentAsync(context.Document, diagnostic, token)), diagnostic);
}
await Task.FromResult(Task.CompletedTask);
}
@bogdanbujdea
bogdanbujdea / UsingsAnalyzerCodeFixProvider.cs
Created April 27, 2018 08:38
UsingsAnalyzerCodeFixProvider
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
try
{
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var usings = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToList();
var orderedUsings = usings.OrderBy(o => new string(o.Name.ToString().TakeWhile(c => c != '.').ToArray()))
.ThenBy(o => o.Name.ToString().Length)
.ToList();
@bogdanbujdea
bogdanbujdea / screenshot.js
Created September 24, 2018 22:39
Node JS script that uses puppeteer to take a screenshot and save it inside a blob from Azure Storage
var path = require('path');
const puppeteer = require('puppeteer');
var azure = require('azure-storage');
var https = require('https');
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//the chart might take a few seconds to load after the page is ready
@bogdanbujdea
bogdanbujdea / Dockerfile
Last active September 25, 2018 00:49
Dockerfile for puppeteer script
FROM node:8-slim
# install chrome rather than relying on Puppeteer
RUN apt-get update && apt-get install -y wget --no-install-recommends \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-unstable --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge --auto-remove -y curl \
@bogdanbujdea
bogdanbujdea / package.json
Created September 24, 2018 23:39
package.json for puppeteer screenshot taker
{
"name": "crybot.imageanalyzer",
"version": "1.0.0",
"private": true,
"dependencies": {
"puppeteer": "^1.8.0",
"azure-storage": "^2.10.1"
}
}
@bogdanbujdea
bogdanbujdea / AzureLogin.cs
Last active September 25, 2018 16:08
Authenticate with Azure
private static IAzure GetAzureContext()
{
IAzure azure = Azure.Authenticate("credentials.json").WithDefaultSubscription();
var currentSubscription = azure.GetCurrentSubscription();
return azure;
}
@bogdanbujdea
bogdanbujdea / credentials.json
Created September 25, 2018 16:08
Credentials file generated by Azure CLI
{
"clientId": "a guid",
"clientSecret": "secret guid",
"subscriptionId": "subscription guid",
"tenantId": "tenant guid",
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
"galleryEndpointUrl": "https://gallery.azure.com/",
@bogdanbujdea
bogdanbujdea / AzureContainerStatus.cs
Created September 25, 2018 16:15
Retrieve the status of a container
//ContainerStatus is an enum that I made, it has just the following values
//Unknown,
//Missing,
//Running,
//Initializing,
//ExceededDuration
public ContainerStatus GetStatus()
{
try